<?php

namespace Tests\Feature\Seeders;

use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Enums\CatalogItemType;
use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\FeaturedGroup;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Tenant\Models\Tenant;
use Database\Seeders\AttributeSeeder;
use Database\Seeders\FiestaFutbolInfantilProductSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class FiestaFutbolInfantilProductSeederTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_seeds_event_items_for_the_new_catalog(): void
    {
        $headerLogo = Attachment::query()->create([
            'path' => 'tests/header.png',
            'filename' => 'header.png',
            'type' => AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);
        $footerLogo = Attachment::query()->create([
            'path' => 'tests/footer.png',
            'filename' => 'footer.png',
            'type' => AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);

        $tenant = Tenant::query()->create([
            'codigo' => 'fiesta_futbol_infantil',
            'nombre' => 'Fiesta Fútbol Infantil',
            'dominio' => 'fiesta-futbol-infantil.localhost',
            'primary_color' => '#00973F',
            'secondary_color' => '#A0A0A0',
            'danger_color' => '#FF8888',
            'success_color' => '#198754',
            'header_bg_color' => '#ffffff',
            'footer_bg_color' => '#015327',
            'header_logo_id' => $headerLogo->id,
            'footer_logo_id' => $footerLogo->id,
        ]);

        $this->seed([
            AttributeSeeder::class,
            FiestaFutbolInfantilProductSeeder::class,
        ]);
        $this->seed([
            AttributeSeeder::class,
            FiestaFutbolInfantilProductSeeder::class,
        ]);

        $this->assertFalse(Attribute::query()
            ->where('tenant_codigo', $tenant->codigo)
            ->whereIn('codigo', ['color', 'talle', 'talle_numerico'])
            ->exists());
        $this->assertSame(
            ['fecha'],
            Attribute::query()->where('tenant_codigo', $tenant->codigo)->pluck('codigo')->all()
        );

        $generalAdmission = CatalogItem::query()
            ->where('tenant_code', $tenant->codigo)
            ->where('slug', 'entrada-general')
            ->with('variants.definitions')
            ->sole();

        $this->assertNull($generalAdmission->inventory_id);
        $this->assertCount(4, $generalAdmission->variants);
        $this->assertEqualsCanonicalizing(
            ['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'],
            $generalAdmission->variants->map(fn ($variant) => $variant->definitions->sole()->value)->all()
        );

        $allDaysItem = CatalogItem::query()
            ->where('tenant_code', $tenant->codigo)
            ->where('nombre', 'Entrada General - Todos los días')
            ->with('bundleComponents.variant.definitions')
            ->sole();

        $this->assertSame(CatalogItemType::Bundle, $allDaysItem->type);
        $this->assertSame('40000.00', $allDaysItem->precio);
        $this->assertNull($allDaysItem->inventory_id);
        $this->assertFalse($allDaysItem->has_tickets);
        $this->assertCount(4, $allDaysItem->bundleComponents);
        $this->assertEqualsCanonicalizing(
            ['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'],
            $allDaysItem->bundleComponents
                ->map(fn ($component) => $component->variant->definitions->sole()->value)
                ->all()
        );

        $foodCombo = CatalogItem::query()
            ->where('tenant_code', $tenant->codigo)
            ->where('nombre', 'Combo 2 Panchos + 2 Hamburguesas')
            ->with('bundleComponents.catalogItem')
            ->sole();

        $this->assertSame(CatalogItemType::Bundle, $foodCombo->type);
        $this->assertSame('24000.00', $foodCombo->precio);
        $this->assertNull($foodCombo->inventory_id);
        $this->assertSame(
            [
                'hamburguesa-papa-frita' => 2,
                'pancho' => 2,
            ],
            $foodCombo->bundleComponents
                ->mapWithKeys(fn ($component): array => [
                    $component->catalogItem->slug => $component->quantity,
                ])
                ->sortKeys()
                ->all()
        );
        $this->assertSame(9, CatalogItem::query()->where('tenant_code', $tenant->codigo)->count());
        $this->assertSame(10, Inventory::query()->count());

        $this->assertSame(
            [
                'Entradas' => ['entrada-general', 'entrada-general-todos-los-dias'],
                'Estacionamiento' => ['estacionamiento-auto', 'estacionamiento-moto'],
                'Comidas' => ['hamburguesa-papa-frita', 'pancho', 'combo-2-panchos-2-hamburguesas'],
                'Bebidas' => ['coca-cola-500ml', 'agua-mineral-1l'],
            ],
            FeaturedGroup::query()
                ->where('tenant_code', $tenant->codigo)
                ->with('featuredItems.catalogItem')
                ->orderBy('group_order')
                ->get()
                ->each(fn (FeaturedGroup $group) => $this->assertSame(
                    GroupLayout::Paginated,
                    $group->group_layout,
                ))
                ->mapWithKeys(fn (FeaturedGroup $group): array => [
                    $group->group_name => $group->featuredItems->pluck('catalogItem.slug')->all(),
                ])
                ->all()
        );
    }
}
