<?php

namespace Tests\Feature\Seeders;

use App\Domains\Tenant\Models\Tenant;
use Database\Seeders\SocialMediaSeeder;
use Database\Seeders\TenantSeeder;
use Database\Seeders\WebsiteTypeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class TenantSeederTest extends TestCase
{
    use RefreshDatabase;

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

        $this->seed([
            WebsiteTypeSeeder::class,
            SocialMediaSeeder::class,
            TenantSeeder::class,
        ]);

        $expectedCodes = ['instagram', 'facebook', 'linkedin', 'whatsapp'];
        $expectedUrls = [
            'https://www.instagram.com/sonderargentina/',
            'https://www.facebook.com/SonderArgentina/',
            'https://www.linkedin.com/company/35664983/',
            'https://wa.me/5493412602222',
        ];

        foreach (['sonder', 'fiesta_futbol_infantil'] as $tenantCode) {
            $socialMedia = Tenant::query()
                ->where('codigo', $tenantCode)
                ->firstOrFail()
                ->socialMedia()
                ->get();

            $this->assertSame($expectedCodes, $socialMedia->pluck('code')->all());
            $this->assertSame($expectedUrls, $socialMedia->pluck('pivot.url')->all());
            $this->assertSame([0, 1, 2, 3], $socialMedia->pluck('pivot.orden')->all());
        }
    }

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

        $this->seed([
            WebsiteTypeSeeder::class,
            SocialMediaSeeder::class,
            TenantSeeder::class,
        ]);

        $sonder = Tenant::query()
            ->where('codigo', 'sonder')
            ->with('websiteExtras.websiteTypeExtra')
            ->sole();

        $this->assertSame('shopit', $sonder->website_type_code);
        $this->assertSame('quantity_and_remove', $sonder->cart_editing_policy->value);
        $this->assertSame('disabled', $sonder->checkout_editing_policy->value);

        $carousel = $sonder->websiteExtras
            ->firstWhere('websiteTypeExtra.codigo', 'carousel');

        $this->assertNotNull($carousel);
        $this->assertCount(6, $carousel->config);

        foreach ($carousel->config as $attachmentId) {
            $this->assertDatabaseHas('attachments', [
                'id' => $attachmentId,
                'type' => 'image',
            ]);
        }

        $fiesta = Tenant::query()
            ->where('codigo', 'fiesta_futbol_infantil')
            ->with('websiteExtras.websiteTypeExtra')
            ->sole();

        $this->assertSame('onticket', $fiesta->website_type_code);
        $this->assertSame('full', $fiesta->cart_editing_policy->value);
        $this->assertSame('disabled', $fiesta->checkout_editing_policy->value);

        $extras = $fiesta->websiteExtras->keyBy('websiteTypeExtra.codigo');
        $this->assertEqualsCanonicalizing(
            ['heroConfig', 'eventConfig', 'additionalInfoConfig'],
            $extras->keys()->all(),
        );

        $heroConfig = $extras->get('heroConfig')->config;
        $this->assertSame('<h1>Fiesta Fútbol Infantil</h1>', $heroConfig['title_html']);
        $this->assertSame(
            '<p>Viví una jornada inolvidable de fútbol infantil.</p>',
            $heroConfig['description_html'],
        );
        $this->assertIsInt($heroConfig['background_image_id']);
        $this->assertDatabaseHas('attachments', [
            'id' => $heroConfig['background_image_id'],
            'type' => 'image',
        ]);

        $this->assertSame([
            'title' => 'Fiesta Fútbol Infantil',
            'location' => 'Rosario, Santa Fe',
            'dates_text' => '9, 10, 11 y 12 de Octubre 2026',
            'dates' => [
                [
                    'date' => '2026-12-05',
                    'start_time' => '09:00',
                    'end_time' => '18:00',
                ],
                [
                    'date' => '2026-12-06',
                    'start_time' => '09:00',
                    'end_time' => '18:00',
                ],
            ],
        ], $extras->get('eventConfig')->config);

        $additionalInfo = $extras->get('additionalInfoConfig')->config['description'];

        $this->assertStringContainsString('class="row g-4 text-start"', $additionalInfo);
        $this->assertSame(2, substr_count($additionalInfo, 'class="col-12 col-md-6"'));
        $this->assertStringContainsString('<strong>Menores y Accesos:</strong>', $additionalInfo);
        $this->assertStringContainsString('<strong>Jugadores y Accesos:</strong>', $additionalInfo);
        $this->assertStringContainsString('<strong>Elementos prohibidos:</strong>', $additionalInfo);
        $this->assertStringContainsString('<strong>Entradas:</strong>', $additionalInfo);
    }
}
