<?php

namespace Tests\Feature\Seeders;

use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Enums\FeaturedGroupSource;
use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Enums\ProductLayout;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\FeaturedGroup;
use App\Domains\Tenant\Models\Tenant;
use Database\Seeders\AttributeSeeder;
use Database\Seeders\BrandSeeder;
use Database\Seeders\CategorySeeder;
use Database\Seeders\ProductCatalogFromImagesSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class ProductCatalogFromImagesSeederTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_creates_paginated_and_random_carousel_groups_for_sonder(): void
    {
        Storage::fake('s3');

        $logo = Attachment::query()->create([
            'path' => 'tests/sonder.png',
            'filename' => 'sonder.png',
            'type' => AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);
        $tenant = Tenant::query()->create([
            'codigo' => 'sonder',
            'nombre' => 'Sonder',
            'dominio' => 'sonder.localhost',
            'primary_color' => '#6376F3',
            'secondary_color' => '#A0A0A0',
            'danger_color' => '#FF8888',
            'success_color' => '#198754',
            'header_bg_color' => '#ffffff',
            'footer_bg_color' => '#313131',
            'header_logo_id' => $logo->id,
            'footer_logo_id' => $logo->id,
        ]);

        $this->seed([
            AttributeSeeder::class,
            CategorySeeder::class,
            BrandSeeder::class,
            ProductCatalogFromImagesSeeder::class,
        ]);

        $groups = FeaturedGroup::query()
            ->where('tenant_code', $tenant->codigo)
            ->with('featuredItems.catalogItem')
            ->orderBy('group_order')
            ->get()
            ->keyBy('group_name');

        $paginatedGroup = $groups->get('Productos');
        $carouselGroup = $groups->get('Productos destacados');
        $catalogItemIds = CatalogItem::query()
            ->where('tenant_code', $tenant->codigo)
            ->orderBy('id')
            ->pluck('id');

        $this->assertNotNull($paginatedGroup);
        $this->assertSame('productos', $paginatedGroup->code);
        $this->assertSame(ProductLayout::ColumnWithImage, $paginatedGroup->product_layout);
        $this->assertSame(GroupLayout::Paginated, $paginatedGroup->group_layout);
        $this->assertSame(FeaturedGroupSource::All, $paginatedGroup->source_type);
        $this->assertSame(2, $paginatedGroup->group_order);
        $this->assertCount(0, $paginatedGroup->featuredItems);

        $this->assertNotNull($carouselGroup);
        $this->assertSame('productos-destacados', $carouselGroup->code);
        $this->assertSame(ProductLayout::ColumnWithImage, $carouselGroup->product_layout);
        $this->assertSame(GroupLayout::Carousel, $carouselGroup->group_layout);
        $this->assertSame(FeaturedGroupSource::Manual, $carouselGroup->source_type);
        $this->assertSame(1, $carouselGroup->group_order);
        $this->assertCount(5, $carouselGroup->featuredItems);
        $this->assertCount(
            5,
            $carouselGroup->featuredItems->pluck('catalog_item_id')->unique(),
        );
        $this->assertCount(
            0,
            $carouselGroup->featuredItems
                ->pluck('catalog_item_id')
                ->diff($catalogItemIds),
        );
        $this->assertSame(
            ['Accesorios', 'Buzos', 'Pantalones', 'Remeras', 'Zapatillas'],
            Category::query()
                ->where('tenant_code', $tenant->codigo)
                ->orderBy('nombre')
                ->pluck('nombre')
                ->all(),
        );
        $this->assertFalse(
            CatalogItem::query()
                ->where('tenant_code', $tenant->codigo)
                ->whereHas('category', fn ($query) => $query->where('tenant_code', '!=', $tenant->codigo))
                ->exists(),
        );
    }
}
