<?php

namespace App\Domains\Tenant\Requests;

use App\Domains\Shared\Rules\ImageOrBase64Rule;
use App\Domains\Tenant\Support\TenantDomainNormalizer;
use Closure;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreTenantRequest extends FormRequest
{
    protected bool $hasInvalidDomain = false;

    public function authorize(): bool
    {
        return true;
    }

    protected function prepareForValidation(): void
    {
        $rawDomain = $this->input('dominio');
        $normalizedDomain = TenantDomainNormalizer::normalize($rawDomain);

        $this->hasInvalidDomain = TenantDomainNormalizer::hasValue($rawDomain)
            && $normalizedDomain === null;

        $this->merge([
            'dominio' => $normalizedDomain,
        ]);
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $logoRule = ['required', new ImageOrBase64Rule];

        return [
            'codigo' => ['required', 'string', 'max:255', Rule::unique('tenants', 'codigo')],
            'nombre' => ['required', 'string', 'max:255'],
            'dominio' => [
                'bail',
                function (string $attribute, mixed $value, Closure $fail): void {
                    if ($this->hasInvalidDomain) {
                        $fail("The {$attribute} field must contain a valid domain or URL.");
                    }
                },
                'required',
                'string',
                'max:255',
                Rule::unique('tenants', 'dominio'),
            ],
            'primary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
            'secondary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
            'danger_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
            'success_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
            'header_bg_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
            'footer_bg_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
            'header_logo' => $logoRule,
            'footer_logo' => $logoRule,
            'hero_bg_image' => ['nullable', new ImageOrBase64Rule],
            'main_carousel_images' => ['sometimes', 'array'],
            'main_carousel_images.*' => ['required', 'distinct', new ImageOrBase64Rule],
            'social_media' => ['sometimes', 'array'],
            'social_media.*.code' => [
                'required',
                'string',
                'distinct',
                Rule::exists('social_media', 'code'),
            ],
            'social_media.*.url' => ['required', 'url', 'max:2048'],
            'social_media.*.orden' => ['sometimes', 'integer', 'min:0', 'distinct'],
            'hero_config' => ['nullable', 'array'],
            'hero_config.title_html' => ['nullable', 'string'],
            'hero_config.description_html' => ['nullable', 'string'],
            'hero_config.button_text' => ['nullable', 'string'],
            'hero_config.button_href' => ['nullable', 'string'],
            'event_config' => ['nullable', 'array'],
            'event_config.title' => ['nullable', 'string'],
            'event_config.location' => ['nullable', 'string'],
            'event_config.dates' => ['nullable', 'array'],
            'event_config.dates.*' => ['required', 'string'],
        ];
    }
}
