<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Proveedor;
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\ProveedorController
 */
final class ProveedorControllerTest extends TestCase
{
    use AdditionalAssertions, RefreshDatabase, WithFaker;

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

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

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


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

    #[Test]
    public function store_saves(): void
    {
        $nombre = fake()->word();

        $response = $this->post(route('proveedors.store'), [
            'nombre' => $nombre,
        ]);

        $proveedors = Proveedor::query()
            ->where('nombre', $nombre)
            ->get();
        $this->assertCount(1, $proveedors);
        $proveedor = $proveedors->first();

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


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

        $response = $this->get(route('proveedors.show', $proveedor));

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


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

    #[Test]
    public function update_behaves_as_expected(): void
    {
        $proveedor = Proveedor::factory()->create();
        $nombre = fake()->word();

        $response = $this->put(route('proveedors.update', $proveedor), [
            'nombre' => $nombre,
        ]);

        $proveedor->refresh();

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

        $this->assertEquals($nombre, $proveedor->nombre);
    }


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

        $response = $this->delete(route('proveedors.destroy', $proveedor));

        $response->assertNoContent();

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