<?php

namespace Database\Seeders;

use App\Domains\Catalog\Models\Attribute;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticket\Models\ValidityTime;
use Illuminate\Database\Seeder;

class AttributeSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $tenants = Tenant::all();

        foreach ($tenants as $tenant) {
            if ($tenant->codigo === 'fiesta_futbol_infantil') {
                $this->seedFiestaFutbolInfantilAttributes($tenant);

                continue;
            }

            $this->seedAttribute($tenant, [
                'codigo' => 'color',
                'nombre' => 'Color',
                'type' => FieldType::Select->value,
                'is_required' => true,
                'metadata_schema' => [
                    'hex' => ['type' => 'string'],
                ],
                'options' => [
                    [
                        'value' => 'Negro',
                        'label' => 'Negro',
                        'sort_order' => 1,
                        'metadata' => ['hex' => '#000000'],
                    ],
                    [
                        'value' => 'Gris',
                        'label' => 'Gris',
                        'sort_order' => 2,
                        'metadata' => ['hex' => '#808080'],
                    ],
                    [
                        'value' => 'Blanco',
                        'label' => 'Blanco',
                        'sort_order' => 3,
                        'metadata' => ['hex' => '#FFFFFF'],
                    ],
                    [
                        'value' => 'Azul',
                        'label' => 'Azul',
                        'sort_order' => 4,
                        'metadata' => ['hex' => '#0000FF'],
                    ],
                ],
            ]);

            // Seed Talle (Size - Text options) attribute
            $this->seedAttribute($tenant, [
                'codigo' => 'talle',
                'nombre' => 'Talle',
                'type' => FieldType::Select->value,
                'is_required' => true,
                'options' => [
                    ['value' => 'S', 'label' => 'S', 'sort_order' => 1],
                    ['value' => 'M', 'label' => 'M', 'sort_order' => 2],
                    ['value' => 'L', 'label' => 'L', 'sort_order' => 3],
                    ['value' => 'XL', 'label' => 'XL', 'sort_order' => 4],
                ],
            ]);

            // Seed Talle Numérico (Numeric Size options) attribute
            $this->seedAttribute($tenant, [
                'codigo' => 'talle_numerico',
                'nombre' => 'Talle Numérico',
                'type' => FieldType::Select->value,
                'is_required' => true,
                'options' => [
                    ['value' => '38', 'label' => '38', 'sort_order' => 1],
                    ['value' => '40', 'label' => '40', 'sort_order' => 2],
                    ['value' => '42', 'label' => '42', 'sort_order' => 3],
                    ['value' => '44', 'label' => '44', 'sort_order' => 4],
                ],
            ]);

            // Las opciones de fecha se resuelven dinÃ¡micamente desde event_dates.
            $this->seedAttribute($tenant, [
                'codigo' => 'event_date',
                'nombre' => 'Fecha',
                'type' => FieldType::EventDate->value,
                'is_required' => true,
            ]);

        }
    }

    private function seedFiestaFutbolInfantilAttributes(Tenant $tenant): void
    {
        Attribute::query()
            ->where('tenant_codigo', $tenant->codigo)
            ->whereNotIn('codigo', [
                'event_date',
                'servicio',
                'color',
                'horario',
                'talle',
                'tipo_alojamiento',
            ])
            ->delete();

        $this->seedAttribute($tenant, [
            'codigo' => 'event_date',
            'nombre' => 'Fecha',
            'type' => FieldType::EventDate->value,
            'is_required' => true,
        ]);

        $breakfastValidityTime = $this->timeWindow('07:00:00', '12:00:00');
        $lunchValidityTime = $this->timeWindow('12:00:00', '15:00:00');
        $dinnerValidityTime = $this->timeWindow('20:00:00', '24:00:00');

        $this->seedAttribute($tenant, [
            'codigo' => 'tipo_alojamiento',
            'nombre' => 'TipoAlojamiento',
            'type' => FieldType::Select->value,
            'is_required' => true,
            'options' => [
                ['value' => 'Carpa', 'label' => 'Carpa', 'sort_order' => 1],
                ['value' => 'Motorhome', 'label' => 'Motorhome', 'sort_order' => 2],
            ],
        ]);

        $this->seedAttribute($tenant, [
            'codigo' => 'servicio',
            'nombre' => 'Servicio',
            'type' => FieldType::Select->value,
            'is_required' => true,
            'options' => [
                ['value' => 'Comedor', 'label' => 'Comedor', 'sort_order' => 1],
                ['value' => 'Vianda', 'label' => 'Vianda', 'sort_order' => 2],
            ],
        ]);

        $this->seedAttribute($tenant, [
            'codigo' => 'color',
            'nombre' => 'Color',
            'type' => FieldType::Select->value,
            'is_required' => true,
            'metadata_schema' => [
                'hex' => ['type' => 'string'],
            ],
            'options' => [
                ['value' => 'Verde', 'label' => 'Verde', 'sort_order' => 1, 'metadata' => ['hex' => '#00973F']],
                ['value' => 'Blanco', 'label' => 'Blanco', 'sort_order' => 2, 'metadata' => ['hex' => '#FFFFFF']],
            ],
        ]);

        $this->seedAttribute($tenant, [
            'codigo' => 'horario',
            'nombre' => 'Horario',
            'type' => FieldType::Select->value,
            'is_required' => true,
            'options' => [
                [
                    'value' => 'Desayuno',
                    'label' => 'Desayuno',
                    'sort_order' => 1,
                    'validity_time_id' => $breakfastValidityTime->id,
                ],
                [
                    'value' => 'Almuerzo',
                    'label' => 'Almuerzo',
                    'sort_order' => 2,
                    'validity_time_id' => $lunchValidityTime->id,
                ],
                [
                    'value' => 'Cena',
                    'label' => 'Cena',
                    'sort_order' => 3,
                    'validity_time_id' => $dinnerValidityTime->id,
                ],
            ],
        ]);

        $this->seedAttribute($tenant, [
            'codigo' => 'talle',
            'nombre' => 'Talle',
            'type' => FieldType::Select->value,
            'is_required' => true,
            'options' => [
                ['value' => '14', 'label' => '14', 'sort_order' => 1],
                ['value' => 'S', 'label' => 'S', 'sort_order' => 2],
                ['value' => 'M', 'label' => 'M', 'sort_order' => 3],
                ['value' => 'L', 'label' => 'L', 'sort_order' => 4],
                ['value' => 'XL', 'label' => 'XL', 'sort_order' => 5],
                ['value' => 'XXL', 'label' => 'XXL', 'sort_order' => 6],
            ],
        ]);
    }

    private function timeWindow(string $startTime, string $endTime): ValidityTime
    {
        return ValidityTime::query()->firstOrCreate([
            'type' => ValidityTimeType::TimeWindow,
            'start_time' => $startTime,
            'end_time' => $endTime,
        ]);
    }

    /**
     * @param  array<string, mixed>  $data
     */
    private function seedAttribute(Tenant $tenant, array $data): void
    {
        $options = $data['options'] ?? [];
        unset($data['options']);

        $attribute = Attribute::query()->updateOrCreate(
            [
                'tenant_codigo' => $tenant->codigo,
                'codigo' => $data['codigo'],
            ],
            $data,
        );

        $attribute->options()->delete();
        $attribute->options()->createMany($options);
    }
}
