<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Compra;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Carbon;
use JMac\Testing\Traits\AdditionalAssertions;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

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

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

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

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


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

    #[Test]
    public function store_saves(): void
    {
        $fecha = Carbon::parse(fake()->dateTime());
        $total = fake()->randomFloat(/** decimal_attributes **/);
        $estado = fake()->randomElement(/** enum_attributes **/);

        $response = $this->post(route('compras.store'), [
            'fecha' => $fecha->toDateTimeString(),
            'total' => $total,
            'estado' => $estado,
        ]);

        $compras = Compra::query()
            ->where('fecha', $fecha)
            ->where('total', $total)
            ->where('estado', $estado)
            ->get();
        $this->assertCount(1, $compras);
        $compra = $compras->first();

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


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

        $response = $this->get(route('compras.show', $compra));

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


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

    #[Test]
    public function update_behaves_as_expected(): void
    {
        $compra = Compra::factory()->create();
        $fecha = Carbon::parse(fake()->dateTime());
        $total = fake()->randomFloat(/** decimal_attributes **/);
        $estado = fake()->randomElement(/** enum_attributes **/);

        $response = $this->put(route('compras.update', $compra), [
            'fecha' => $fecha->toDateTimeString(),
            'total' => $total,
            'estado' => $estado,
        ]);

        $compra->refresh();

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

        $this->assertEquals($fecha, $compra->fecha);
        $this->assertEquals($total, $compra->total);
        $this->assertEquals($estado, $compra->estado);
    }


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

        $response = $this->delete(route('compras.destroy', $compra));

        $response->assertNoContent();

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