<?php

namespace Database\Seeders;

use App\Domains\Tenant\Services\WebsiteTypeService;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use RuntimeException;

class WebsiteTypeSeeder extends Seeder
{
    private const PRESENTATION = [
        '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',
    ];

    public function __construct(private readonly WebsiteTypeService $websiteTypeService) {}

    public function run(): void
    {
        $shopIt = $this->websiteTypeService->updateOrCreate(
            ['codigo' => 'shopit'],
            [
                'nombre' => 'ShopIt',
                'dominio' => 'localhost',
                'scanner_domain' => 'scanner.localhost',
                'site_title' => 'ShopIt',
                ...self::PRESENTATION,
                'site_logo' => $this->onTicketLogo(),
                'footer_logo' => $this->onTicketFooterLogo(),
                'favicon' => $this->onTicketFavicon(),
            ],
        );

        $shopIt->extras()->updateOrCreate(
            ['codigo' => 'carousel'],
            [
                'nombre' => 'Carrusel principal',
                'descripcion' => 'Listado de attachments que se muestran en el carousel principal.',
                'is_required' => false,
                'config_schema' => [
                    'request_rules' => [
                        '$' => 'required|array|max:10',
                        '$.*' => 'required|image_or_base64|distinct',
                    ],
                    'transforms' => [
                        '$.*' => [
                            'handler' => 'attachment',
                            'attachment_type' => 'image',
                        ],
                    ],
                ],
            ],
        );

        $onTicket = $this->websiteTypeService->updateOrCreate(
            ['codigo' => 'onticket'],
            [
                'nombre' => 'OnTicket',
                'dominio' => 'onticket.localhost',
                'scanner_domain' => 'scanner.onticket.localhost',
                'site_title' => 'OnTicket',
                ...self::PRESENTATION,
                'site_logo' => $this->onTicketLogo(),
                'footer_logo' => $this->onTicketFooterLogo(),
                'favicon' => $shopIt->favicon()->firstOrFail()->key,
            ],
        );

        $onTicket->extras()->updateOrCreate(
            ['codigo' => 'heroConfig'],
            [
                'nombre' => 'Configuración del hero',
                'descripcion' => 'Configuracion del hero principal del evento.',
                'is_required' => false,
                'config_schema' => [
                    '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',
                        ],
                    ],
                ],
            ],
        );

        $onTicket->extras()->updateOrCreate(
            ['codigo' => 'eventConfig'],
            [
                'nombre' => 'Información del evento',
                'descripcion' => 'Informacion principal del evento.',
                'is_required' => false,
                'config_schema' => [
                    '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' => [],
                ],
            ],
        );

        $onTicket->extras()->updateOrCreate(
            ['codigo' => 'additionalInfoConfig'],
            [
                'nombre' => 'Información adicional',
                'descripcion' => 'Información adicional visible en la página del evento.',
                'is_required' => false,
                'config_schema' => [
                    'request_rules' => [
                        '$' => 'required|array',
                        'description' => 'nullable|string',
                    ],
                    'transforms' => [],
                ],
            ],
        );
    }

    private function onTicketLogo(): UploadedFile
    {
        $path = public_path('images/website_types/onticket_logo.png');

        if (! file_exists($path)) {
            throw new RuntimeException("OnTicket logo not found at path: {$path}");
        }

        return new UploadedFile(
            $path,
            'onticket_logo.png',
            'image/png',
            null,
            true,
        );
    }

    private function onTicketFooterLogo(): UploadedFile
    {
        $path = public_path('images/website_types/onticket_footer_logo.png');

        if (! file_exists($path)) {
            throw new RuntimeException("OnTicket footer logo not found at path: {$path}");
        }

        return new UploadedFile(
            $path,
            'onticket_footer_logo.png',
            'image/png',
            null,
            true,
        );
    }

    private function onTicketFavicon(): UploadedFile
    {
        $path = public_path('images/website_types/onticket_favicon.svg');

        if (! file_exists($path)) {
            throw new RuntimeException("OnTicket favicon not found at path: {$path}");
        }

        return new UploadedFile(
            $path,
            'onticket_favicon.svg',
            'image/svg+xml',
            null,
            true,
        );
    }
}
