<?php

namespace Tests\Feature\Catalog;

use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Models\Product;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;

class ProductVariantAttachmentTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_can_associate_attachments_to_product_variants(): void
    {
        // 1. Create Tenant
        $hdrKey = (string) Str::uuid();
        $ftrKey = (string) Str::uuid();
        $headerAttachment = Attachment::create([
            'key' => $hdrKey,
            'path' => 'tenants/' . $hdrKey . '.png',
            'filename' => 'logo_header.png',
            'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);
        $footerAttachment = Attachment::create([
            'key' => $ftrKey,
            'path' => 'tenants/' . $ftrKey . '.png',
            'filename' => 'logo_footer.png',
            'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);

        $tenant = Tenant::create([
            'codigo' => 'acme',
            'nombre' => 'Acme Inc.',
            'dominio' => 'acme.com',
            'primary_color' => '#ffffff',
            'secondary_color' => '#ffffff',
            'danger_color' => '#ffffff',
            'header_footer_bg_color' => '#ffffff',
            'header_logo_id' => $headerAttachment->id,
            'footer_logo_id' => $footerAttachment->id,
        ]);

        // 2. Create Product
        $product = Product::create([
            'tenant_codigo' => $tenant->codigo,
            'categoria_id' => 1,
            'slug' => 'test-product',
            'nombre' => 'Test Product',
            'descripcion' => 'A test product description',
            'precio' => 99.99,
        ]);

        // 3. Create Variant
        $variant = ProductVariant::create([
            'producto_id' => $product->id,
            'slug' => 'test-variant-1',
            'nombre' => 'Test Variant 1',
            'stock' => 10,
            'precio' => 99.99,
        ]);

        // 4. Create Attachments
        $attachment1 = Attachment::create([
            'key' => (string) Str::uuid(),
            'path' => 'attachments/image1.png',
            'filename' => 'image1.png',
            'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);

        $attachment2 = Attachment::create([
            'key' => (string) Str::uuid(),
            'path' => 'attachments/image2.png',
            'filename' => 'image2.png',
            'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);

        // 5. Associate
        $variant->attachments()->attach([$attachment1->id, $attachment2->id]);

        // 6. Assert relations
        $this->assertCount(2, $variant->attachments);
        $this->assertTrue($variant->attachments->contains($attachment1));
        $this->assertTrue($variant->attachments->contains($attachment2));
    }
}
