<?php

namespace Tests\Unit\Sale;

use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Models\PurchaseItem;
use App\Domains\Sale\Resources\AdminApp\SaleDetailResource;
use Illuminate\Http\Request;
use Tests\TestCase;

class SaleDetailResourceTest extends TestCase
{
    public function test_it_serializes_the_purchased_item_snapshot(): void
    {
        $purchase = (new Purchase)->forceFill([
            'id' => 15,
            'total' => '30000.00',
        ]);
        $item = (new PurchaseItem)->forceFill([
            'id' => 8,
            'item_nombre' => 'Comida',
            'variant_attributes' => [
                ['name' => 'Fecha', 'value' => ['2026-10-12']],
            ],
            'cantidad' => 3,
            'precio_unitario' => '10000.00',
            'total' => '30000.00',
        ]);
        $purchase->setRelation('items', collect([$item]));

        $data = (new SaleDetailResource($purchase))->resolve(Request::create('/'));

        $this->assertSame(15, $data['id']);
        $this->assertSame('Comida', $data['items'][0]['product']);
        $this->assertSame(['2026-10-12'], $data['items'][0]['event_dates']);
        $this->assertSame(3, $data['items'][0]['quantity']);
        $this->assertSame('10000.00', $data['items'][0]['unit_price']);
        $this->assertSame('30000.00', $data['items'][0]['total']);
        $this->assertSame('30000.00', $data['total']);
    }

    public function test_it_does_not_fall_back_to_cart_items_when_the_purchase_has_no_items(): void
    {
        $purchase = (new Purchase)->forceFill([
            'id' => 16,
            'total' => '25000.00',
        ]);
        $purchase->setRelation('items', collect());

        $data = (new SaleDetailResource($purchase))->resolve(Request::create('/'));

        $this->assertCount(0, $data['items']);
    }
}
