<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Compra;
use App\Models\DetalleCompra;
use App\Models\Producto;
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\DetalleCompraController
 */
final class DetalleCompraControllerTest extends TestCase
{
    use AdditionalAssertions, RefreshDatabase, WithFaker;

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

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

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


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

    #[Test]
    public function store_saves(): void
    {
        $compra = Compra::factory()->create();
        $producto = Producto::factory()->create();
        $cantidad = fake()->numberBetween(-10000, 10000);
        $precio_unitario = fake()->randomFloat(/** decimal_attributes **/);
        $subtotal = fake()->randomFloat(/** decimal_attributes **/);

        $response = $this->post(route('detalle-compras.store'), [
            'compra_id' => $compra->id,
            'producto_id' => $producto->id,
            'cantidad' => $cantidad,
            'precio_unitario' => $precio_unitario,
            'subtotal' => $subtotal,
        ]);

        $detalleCompras = DetalleCompra::query()
            ->where('compra_id', $compra->id)
            ->where('producto_id', $producto->id)
            ->where('cantidad', $cantidad)
            ->where('precio_unitario', $precio_unitario)
            ->where('subtotal', $subtotal)
            ->get();
        $this->assertCount(1, $detalleCompras);
        $detalleCompra = $detalleCompras->first();

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


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

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

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


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

    #[Test]
    public function update_behaves_as_expected(): void
    {
        $detalleCompra = DetalleCompra::factory()->create();
        $compra = Compra::factory()->create();
        $producto = Producto::factory()->create();
        $cantidad = fake()->numberBetween(-10000, 10000);
        $precio_unitario = fake()->randomFloat(/** decimal_attributes **/);
        $subtotal = fake()->randomFloat(/** decimal_attributes **/);

        $response = $this->put(route('detalle-compras.update', $detalleCompra), [
            'compra_id' => $compra->id,
            'producto_id' => $producto->id,
            'cantidad' => $cantidad,
            'precio_unitario' => $precio_unitario,
            'subtotal' => $subtotal,
        ]);

        $detalleCompra->refresh();

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

        $this->assertEquals($compra->id, $detalleCompra->compra_id);
        $this->assertEquals($producto->id, $detalleCompra->producto_id);
        $this->assertEquals($cantidad, $detalleCompra->cantidad);
        $this->assertEquals($precio_unitario, $detalleCompra->precio_unitario);
        $this->assertEquals($subtotal, $detalleCompra->subtotal);
    }


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

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

        $response->assertNoContent();

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