<?php

namespace Tests\Feature\Seeders;

use App\Domains\Attachable\Models\Attachment;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\WebsiteTypeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class WebsiteTypeSeederTest extends TestCase
{
    use RefreshDatabase;

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

        $this->seed(WebsiteTypeSeeder::class);
        $this->seed(WebsiteTypeSeeder::class);

        $this->assertSame(2, WebsiteType::query()->count());
        $this->assertSame(5, Attachment::query()->count());

        $expectedPresentation = [
            'primary_color' => '#FF7006',
            'secondary_color' => '#777777',
            'danger_color' => '#E04A4A',
            'success_color' => '#81BC73',
            'warning_color' => '#81BC73',
            'body_color' => '#666666',
            'darker_body_color' => '#333333',
            'surface_color' => '#ffffff',
            'background_color' => '#f8f8f8',
            'border_color' => '#EAEAEA',
        ];

        $shopIt = WebsiteType::query()
            ->where('codigo', 'shopit')
            ->with('extras')
            ->sole();

        $this->assertSame('ShopIt', $shopIt->nombre);
        $this->assertSame('localhost', $shopIt->dominio);
        $this->assertSame('scanner.localhost', $shopIt->scanner_domain);
        $this->assertSame($expectedPresentation, $shopIt->only(array_keys($expectedPresentation)));
        $this->assertSame('onticket_logo.png', $shopIt->siteLogo->filename);
        Storage::disk('s3')->assertExists($shopIt->siteLogo->path);
        $this->assertSame('onticket_footer_logo.png', $shopIt->footerLogo->filename);
        Storage::disk('s3')->assertExists($shopIt->footerLogo->path);
        $this->assertSame('onticket_favicon.svg', $shopIt->favicon->filename);
        Storage::disk('s3')->assertExists($shopIt->favicon->path);
        $this->assertSame(['carousel'], $shopIt->extras->pluck('codigo')->all());
        $this->assertSame('Carrusel principal', $shopIt->extras->sole()->nombre);
        $this->assertSame([
            'request_rules' => [
                '$' => 'required|array|max:10',
                '$.*' => 'required|image_or_base64|distinct',
            ],
            'transforms' => [
                '$.*' => [
                    'handler' => 'attachment',
                    'attachment_type' => 'image',
                ],
            ],
        ], $shopIt->extras->sole()->config_schema);

        $onTicket = WebsiteType::query()
            ->where('codigo', 'onticket')
            ->with('extras')
            ->sole();

        $this->assertSame('OnTicket', $onTicket->nombre);
        $this->assertSame('onticket.localhost', $onTicket->dominio);
        $this->assertSame('scanner.onticket.localhost', $onTicket->scanner_domain);
        $this->assertSame($expectedPresentation, $onTicket->only(array_keys($expectedPresentation)));
        $this->assertSame('onticket_logo.png', $onTicket->siteLogo->filename);
        Storage::disk('s3')->assertExists($onTicket->siteLogo->path);
        $this->assertSame('onticket_footer_logo.png', $onTicket->footerLogo->filename);
        Storage::disk('s3')->assertExists($onTicket->footerLogo->path);
        $this->assertNotSame($shopIt->site_logo, $onTicket->site_logo);
        $this->assertNotSame($shopIt->footer_logo, $onTicket->footer_logo);
        $this->assertSame($shopIt->favicon_id, $onTicket->favicon_id);
        $this->assertSame('onticket_favicon.svg', $onTicket->favicon->filename);
        $this->assertEqualsCanonicalizing(
            ['heroConfig', 'eventConfig', 'additionalInfoConfig'],
            $onTicket->extras->pluck('codigo')->all(),
        );

        $heroSchema = $onTicket->extras->firstWhere('codigo', 'heroConfig')->config_schema;
        $this->assertSame([
            'request_rules' => [
                '$' => 'required|array',
                'title_html' => 'nullable|string',
                'description_html' => 'nullable|string',
                'button_text' => 'nullable|string',
                'button_href' => 'nullable|string',
                'background_image_id' => 'nullable|cropped_image_or_base64',
            ],
            'transforms' => [
                'background_image_id' => [
                    'handler' => 'attachment',
                    'attachment_type' => 'image',
                ],
            ],
        ], $heroSchema);

        $eventSchema = $onTicket->extras->firstWhere('codigo', 'eventConfig')->config_schema;
        $this->assertSame([
            'request_rules' => [
                '$' => 'required|array',
                'title' => 'nullable|string',
                'location' => 'nullable|string',
                'dates_text' => 'nullable|string|max:255',
                'dates' => 'nullable|array',
                'dates.*' => 'required|array:date,start_time,end_time',
                'dates.*.date' => 'required|date_format:Y-m-d|distinct',
                'dates.*.start_time' => 'required|date_format:H:i',
                'dates.*.end_time' => 'required|date_format:H:i',
                'contact' => 'nullable|array:whatsapp_url,instagram_url,facebook_url',
                'contact.whatsapp_url' => 'nullable|url|max:2048',
                'contact.instagram_url' => 'nullable|url|max:2048',
                'contact.facebook_url' => 'nullable|url|max:2048',
            ],
            'transforms' => [],
        ], $eventSchema);

        $additionalInfoSchema = $onTicket->extras
            ->firstWhere('codigo', 'additionalInfoConfig')
            ->config_schema;
        $this->assertSame([
            'request_rules' => [
                '$' => 'required|array',
                'description' => 'nullable|string',
            ],
            'transforms' => [],
        ], $additionalInfoSchema);
    }
}
