<?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' => '#f8f8f8',
    ];

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

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

        $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',
                ...self::PRESENTATION,
                'site_logo' => $this->onTicketLogo(),
            ],
        );

        $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|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|date_format:Y-m-d|distinct',
                    ],
                    'transforms' => [],
                ],
            ],
        );
    }

    private function onTicketLogo(): UploadedFile
    {
        $path = public_path('images/website_types/shopit_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,
        );
    }
}
