<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\DetalleVenta;
use App\Models\DetalleVentum;
use App\Models\Producto;
use App\Models\Venta;
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\DetalleVentaController
 */
final class DetalleVentaControllerTest extends TestCase
{
    use AdditionalAssertions, RefreshDatabase, WithFaker;

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

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

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


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

    #[Test]
    public function store_saves(): void
    {
        $venta = Venta::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-venta.store'), [
            'venta_id' => $venta->id,
            'producto_id' => $producto->id,
            'cantidad' => $cantidad,
            'precio_unitario' => $precio_unitario,
            'subtotal' => $subtotal,
        ]);

        $detalleVenta = DetalleVentum::query()
            ->where('venta_id', $venta->id)
            ->where('producto_id', $producto->id)
            ->where('cantidad', $cantidad)
            ->where('precio_unitario', $precio_unitario)
            ->where('subtotal', $subtotal)
            ->get();
        $this->assertCount(1, $detalleVenta);
        $detalleVentum = $detalleVenta->first();

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


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

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

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


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

    #[Test]
    public function update_behaves_as_expected(): void
    {
        $detalleVentum = DetalleVenta::factory()->create();
        $venta = Venta::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-venta.update', $detalleVentum), [
            'venta_id' => $venta->id,
            'producto_id' => $producto->id,
            'cantidad' => $cantidad,
            'precio_unitario' => $precio_unitario,
            'subtotal' => $subtotal,
        ]);

        $detalleVentum->refresh();

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

        $this->assertEquals($venta->id, $detalleVentum->venta_id);
        $this->assertEquals($producto->id, $detalleVentum->producto_id);
        $this->assertEquals($cantidad, $detalleVentum->cantidad);
        $this->assertEquals($precio_unitario, $detalleVentum->precio_unitario);
        $this->assertEquals($subtotal, $detalleVentum->subtotal);
    }


    #[Test]
    public function destroy_deletes_and_responds_with(): void
    {
        $detalleVentum = DetalleVenta::factory()->create();
        $detalleVentum = DetalleVentum::factory()->create();

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

        $response->assertNoContent();

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