<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Venta;
use App\Models\Ventum;
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\VentaController
 */
final class VentaControllerTest extends TestCase
{
    use AdditionalAssertions, RefreshDatabase, WithFaker;

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

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

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


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

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

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

        $venta = Ventum::query()
            ->where('fecha', $fecha)
            ->where('total', $total)
            ->where('tipo_pago', $tipo_pago)
            ->where('estado', $estado)
            ->get();
        $this->assertCount(1, $venta);
        $ventum = $venta->first();

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


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

        $response = $this->get(route('venta.show', $ventum));

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


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

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

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

        $ventum->refresh();

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

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


    #[Test]
    public function destroy_deletes_and_responds_with(): void
    {
        $ventum = Venta::factory()->create();
        $ventum = Ventum::factory()->create();

        $response = $this->delete(route('venta.destroy', $ventum));

        $response->assertNoContent();

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