<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Caja;
use App\Models\MovimientoCaja;
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\MovimientoCajaController
 */
final class MovimientoCajaControllerTest extends TestCase
{
    use AdditionalAssertions, RefreshDatabase, WithFaker;

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

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

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


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

    #[Test]
    public function store_saves(): void
    {
        $caja = Caja::factory()->create();
        $tipo = fake()->randomElement(/** enum_attributes **/);
        $monto = fake()->randomFloat(/** decimal_attributes **/);
        $fecha = Carbon::parse(fake()->dateTime());

        $response = $this->post(route('movimiento-cajas.store'), [
            'caja_id' => $caja->id,
            'tipo' => $tipo,
            'monto' => $monto,
            'fecha' => $fecha->toDateTimeString(),
        ]);

        $movimientoCajas = MovimientoCaja::query()
            ->where('caja_id', $caja->id)
            ->where('tipo', $tipo)
            ->where('monto', $monto)
            ->where('fecha', $fecha)
            ->get();
        $this->assertCount(1, $movimientoCajas);
        $movimientoCaja = $movimientoCajas->first();

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


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

        $response = $this->get(route('movimiento-cajas.show', $movimientoCaja));

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


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

    #[Test]
    public function update_behaves_as_expected(): void
    {
        $movimientoCaja = MovimientoCaja::factory()->create();
        $caja = Caja::factory()->create();
        $tipo = fake()->randomElement(/** enum_attributes **/);
        $monto = fake()->randomFloat(/** decimal_attributes **/);
        $fecha = Carbon::parse(fake()->dateTime());

        $response = $this->put(route('movimiento-cajas.update', $movimientoCaja), [
            'caja_id' => $caja->id,
            'tipo' => $tipo,
            'monto' => $monto,
            'fecha' => $fecha->toDateTimeString(),
        ]);

        $movimientoCaja->refresh();

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

        $this->assertEquals($caja->id, $movimientoCaja->caja_id);
        $this->assertEquals($tipo, $movimientoCaja->tipo);
        $this->assertEquals($monto, $movimientoCaja->monto);
        $this->assertEquals($fecha, $movimientoCaja->fecha);
    }


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

        $response = $this->delete(route('movimiento-cajas.destroy', $movimientoCaja));

        $response->assertNoContent();

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