<?php

namespace Tests\Feature\Http\Controllers;

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

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

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

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


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

    #[Test]
    public function store_saves(): void
    {
        $nombre = fake()->word();
        $tipo_documento = fake()->randomElement(/** enum_attributes **/);
        $numero_documento = fake()->word();

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

        $clientes = Cliente::query()
            ->where('nombre', $nombre)
            ->where('tipo_documento', $tipo_documento)
            ->where('numero_documento', $numero_documento)
            ->get();
        $this->assertCount(1, $clientes);
        $cliente = $clientes->first();

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


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

        $response = $this->get(route('clientes.show', $cliente));

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


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

    #[Test]
    public function update_behaves_as_expected(): void
    {
        $cliente = Cliente::factory()->create();
        $nombre = fake()->word();
        $tipo_documento = fake()->randomElement(/** enum_attributes **/);
        $numero_documento = fake()->word();

        $response = $this->put(route('clientes.update', $cliente), [
            'nombre' => $nombre,
            'tipo_documento' => $tipo_documento,
            'numero_documento' => $numero_documento,
        ]);

        $cliente->refresh();

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

        $this->assertEquals($nombre, $cliente->nombre);
        $this->assertEquals($tipo_documento, $cliente->tipo_documento);
        $this->assertEquals($numero_documento, $cliente->numero_documento);
    }


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

        $response = $this->delete(route('clientes.destroy', $cliente));

        $response->assertNoContent();

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