<?php

namespace App\Http\Requests\Tenant;

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

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $tenantId = request()->attributes->get('tenant_id');
        $creating = $this->isMethod('post');

        return [
            'name' => [$creating ? 'required' : 'sometimes', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'event_date' => $creating ? ['required', 'date', 'after_or_equal:today'] : ['sometimes', 'date'],
            'location' => ['nullable', 'string', 'max:255'],
            'event_type_id' => [$creating ? 'required' : 'sometimes', 'exists:event_types,id'],
            'package_id' => ['nullable', 'integer', 'exists:packages,id'],
            'service_package_slug' => [
                'nullable',
                'string',
                'max:64',
                Rule::exists('packages', 'slug')->where('type', 'endcustomer'),
            ],
            'max_participants' => ['nullable', 'integer', 'min:1', 'max:10000'],
            'public_url' => ['nullable', 'url', 'max:500'],
            'custom_domain' => ['nullable', 'string', 'max:255'],
            'theme_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
            'logo_image' => ['nullable', 'image', 'max:2048'], // 2MB
            'cover_image' => ['nullable', 'image', 'max:5120'], // 5MB
            'password_protected' => ['nullable', 'boolean'],
            'password' => ['required_if:password_protected,true', 'string', 'min:6', 'confirmed'],
            'status' => ['nullable', Rule::in(['draft', 'published', 'archived'])],
            'features' => ['nullable', 'array'],
            'features.*' => ['string'],
            'settings' => ['nullable', 'array'],
            'settings.location' => ['nullable', 'string', 'max:255'],
            'settings.branding' => ['nullable', 'array'],
            'settings.branding.*' => ['nullable'],
            'settings.engagement_mode' => ['nullable', Rule::in(['tasks', 'photo_only'])],
            'settings.guest_downloads_enabled' => ['nullable', 'boolean'],
            'settings.guest_sharing_enabled' => ['nullable', 'boolean'],
            'settings.guest_upload_visibility' => ['nullable', Rule::in(['review', 'immediate'])],
            'settings.live_show' => ['nullable', 'array'],
            'settings.live_show.moderation_mode' => ['nullable', Rule::in(['off', 'manual', 'trusted_only'])],
            'settings.live_show.retention_window_hours' => ['nullable', 'integer', 'min:1', 'max:72'],
            'settings.live_show.playback_mode' => ['nullable', Rule::in(['newest_first', 'balanced', 'curated'])],
            'settings.live_show.pace_mode' => ['nullable', Rule::in(['auto', 'fixed'])],
            'settings.live_show.fixed_interval_seconds' => ['nullable', 'integer', 'min:3', 'max:20'],
            'settings.live_show.layout_mode' => ['nullable', Rule::in(['single', 'split', 'grid_burst'])],
            'settings.live_show.effect_preset' => ['nullable', Rule::in([
                'film_cut',
                'shutter_flash',
                'polaroid_toss',
                'parallax_glide',
                'light_effects',
            ])],
            'settings.live_show.effect_intensity' => ['nullable', 'integer', 'min:0', 'max:100'],
            'settings.live_show.background_mode' => ['nullable', Rule::in(['blur_last', 'gradient', 'solid', 'brand'])],
            'settings.watermark' => ['nullable', 'array'],
            'settings.watermark.mode' => ['nullable', Rule::in(['base', 'custom', 'off'])],
            'settings.watermark.asset' => ['nullable', 'string', 'max:500'],
            'settings.watermark.asset_data_url' => ['nullable', 'string'],
            'settings.watermark.position' => ['nullable', Rule::in([
                'top-left',
                'top-center',
                'top-right',
                'middle-left',
                'center',
                'middle-right',
                'bottom-left',
                'bottom-center',
                'bottom-right',
            ])],
            'settings.watermark.opacity' => ['nullable', 'numeric', 'min:0', 'max:1'],
            'settings.watermark.scale' => ['nullable', 'numeric', 'min:0.05', 'max:1'],
            'settings.watermark.padding' => ['nullable', 'integer', 'min:0', 'max:500'],
            'settings.watermark.offset_x' => ['nullable', 'integer', 'min:-500', 'max:500'],
            'settings.watermark.offset_y' => ['nullable', 'integer', 'min:-500', 'max:500'],
            'settings.watermark_serve_originals' => ['nullable', 'boolean'],
            'accepted_waiver' => ['nullable', 'boolean'],
        ];
    }

    /**
     * Get custom validation messages.
     */
    public function messages(): array
    {
        return [
            'event_date.after_or_equal' => 'Das Event-Datum darf nicht in der Vergangenheit liegen.',
            'password.confirmed' => 'Die Passwortbestätigung stimmt nicht überein.',
            'logo_image.image' => 'Das Logo muss ein Bild sein.',
            'cover_image.image' => 'Das Cover-Bild muss ein Bild sein.',
            'theme_color.regex' => 'Die Farbe muss im Hex-Format angegeben werden (z.B. #FF0000).',
        ];
    }

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