<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Categoria;
use App\Models\Categorium;
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\CategoriaController
 */
final class CategoriaControllerTest extends TestCase
{
    use AdditionalAssertions, RefreshDatabase, WithFaker;

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

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

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


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

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

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

        $categoria = Categorium::query()
            ->where('nombre', $nombre)
            ->get();
        $this->assertCount(1, $categoria);
        $categorium = $categoria->first();

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


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

        $response = $this->get(route('categoria.show', $categorium));

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


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

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

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

        $categorium->refresh();

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

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


    #[Test]
    public function destroy_deletes_and_responds_with(): void
    {
        $categorium = Categoria::factory()->create();
        $categorium = Categorium::factory()->create();

        $response = $this->delete(route('categoria.destroy', $categorium));

        $response->assertNoContent();

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