<?php

namespace App\Http\Requests\Tenant;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class SettingsStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'settings' => ['required', 'array'],
            'settings.branding' => ['sometimes', 'array'],
            'settings.branding.logo_url' => ['nullable', 'url', 'max:500'],
            'settings.branding.primary_color' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.secondary_color' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.font_family' => ['nullable', 'string', 'max:100'],
            'settings.branding.surface_color' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.mode' => ['nullable', Rule::in(['light', 'dark', 'auto'])],
            'settings.branding.use_default_branding' => ['nullable', 'boolean'],
            'settings.branding.palette' => ['nullable', 'array'],
            'settings.branding.palette.primary' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.palette.secondary' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.palette.background' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.palette.surface' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.typography' => ['nullable', 'array'],
            'settings.branding.typography.heading' => ['nullable', 'string', 'max:150'],
            'settings.branding.typography.body' => ['nullable', 'string', 'max:150'],
            'settings.branding.typography.size' => ['nullable', Rule::in(['s', 'm', 'l'])],
            'settings.branding.logo' => ['nullable', 'array'],
            'settings.branding.logo.mode' => ['nullable', Rule::in(['emoticon', 'upload'])],
            'settings.branding.logo.value' => ['nullable', 'string', 'max:500'],
            'settings.branding.logo.position' => ['nullable', Rule::in(['left', 'right', 'center'])],
            'settings.branding.logo.size' => ['nullable', Rule::in(['s', 'm', 'l'])],
            'settings.branding.buttons' => ['nullable', 'array'],
            'settings.branding.buttons.style' => ['nullable', Rule::in(['filled', 'outline'])],
            'settings.branding.buttons.radius' => ['nullable', 'integer', 'min:0', 'max:64'],
            'settings.branding.buttons.primary' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.buttons.secondary' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.branding.buttons.link_color' => ['nullable', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'settings.features' => ['sometimes', 'array'],
            'settings.features.photo_likes_enabled' => ['nullable', 'boolean'],
            'settings.features.event_checklist' => ['nullable', 'boolean'],
            'settings.features.custom_domain' => ['nullable', 'boolean'],
            'settings.features.advanced_analytics' => ['nullable', 'boolean'],
            'settings.custom_domain' => ['nullable', 'string', 'max:255', 'regex:/^(?!-)(?:[a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,}$/'],
            'settings.contact_email' => ['nullable', 'email', 'max:255'],
            'settings.event_default_type' => ['nullable', 'string', 'max:50'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'settings.required' => 'Settings-Daten sind erforderlich.',
            'settings.branding.logo_url.url' => 'Die Logo-URL muss eine gOltige URL sein.',
            'settings.branding.primary_color.regex' => 'Die Prim??rfarbe muss ein gOltiges Hex-Format (#RRGGBB) haben.',
            'settings.branding.secondary_color.regex' => 'Die Sekund??rfarbe muss ein gOltiges Hex-Format (#RRGGBB) haben.',
            'settings.custom_domain.regex' => 'Das Custom Domain muss ein gOltiges Domain-Format haben.',
            'settings.contact_email.email' => 'Die Kontakt-E-Mail muss eine gOltige E-Mail-Adresse sein.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation()
    {
        $this->merge([
            'settings' => $this->input('settings', []),
        ]);
    }
}

