<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Inventario;
use App\Models\Producto;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use JMac\Testing\Traits\AdditionalAssertions;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

/**
 * @see \App\Http\Controllers\InventarioController
 */
final class InventarioControllerTest extends TestCase
{
    use AdditionalAssertions, RefreshDatabase, WithFaker;

    #[Test]
    public function index_behaves_as_expected(): void
    {
        $inventarios = Inventario::factory()->count(3)->create();

        $response = $this->get(route('inventarios.index'));

        $response->assertOk();
        $response->assertJsonStructure([]);
    }


    #[Test]
    public function store_uses_form_request_validation(): void
    {
        $this->assertActionUsesFormRequest(
            \App\Http\Controllers\InventarioController::class,
            'store',
            \App\Http\Requests\InventarioStoreRequest::class
        );
    }

    #[Test]
    public function store_saves(): void
    {
        $producto = Producto::factory()->create();
        $stock_actual = fake()->numberBetween(-10000, 10000);
        $stock_minimo = fake()->numberBetween(-10000, 10000);

        $response = $this->post(route('inventarios.store'), [
            'producto_id' => $producto->id,
            'stock_actual' => $stock_actual,
            'stock_minimo' => $stock_minimo,
        ]);

        $inventarios = Inventario::query()
            ->where('producto_id', $producto->id)
            ->where('stock_actual', $stock_actual)
            ->where('stock_minimo', $stock_minimo)
            ->get();
        $this->assertCount(1, $inventarios);
        $inventario = $inventarios->first();

        $response->assertCreated();
        $response->assertJsonStructure([]);
    }


    #[Test]
    public function show_behaves_as_expected(): void
    {
        $inventario = Inventario::factory()->create();

        $response = $this->get(route('inventarios.show', $inventario));

        $response->assertOk();
        $response->assertJsonStructure([]);
    }


    #[Test]
    public function update_uses_form_request_validation(): void
    {
        $this->assertActionUsesFormRequest(
            \App\Http\Controllers\InventarioController::class,
            'update',
            \App\Http\Requests\InventarioUpdateRequest::class
        );
    }

    #[Test]
    public function update_behaves_as_expected(): void
    {
        $inventario = Inventario::factory()->create();
        $producto = Producto::factory()->create();
        $stock_actual = fake()->numberBetween(-10000, 10000);
        $stock_minimo = fake()->numberBetween(-10000, 10000);

        $response = $this->put(route('inventarios.update', $inventario), [
            'producto_id' => $producto->id,
            'stock_actual' => $stock_actual,
            'stock_minimo' => $stock_minimo,
        ]);

        $inventario->refresh();

        $response->assertOk();
        $response->assertJsonStructure([]);

        $this->assertEquals($producto->id, $inventario->producto_id);
        $this->assertEquals($stock_actual, $inventario->stock_actual);
        $this->assertEquals($stock_minimo, $inventario->stock_minimo);
    }


    #[Test]
    public function destroy_deletes_and_responds_with(): void
    {
        $inventario = Inventario::factory()->create();

        $response = $this->delete(route('inventarios.destroy', $inventario));

        $response->assertNoContent();

        $this->assertModelMissing($inventario);
    }
}
