<?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');

        return [
            'name' => ['required', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'event_date' => ['required', 'date', 'after_or_equal:today'],
            'location' => ['nullable', 'string', 'max:255'],
            'event_type_id' => ['required', 'exists:event_types,id'],
            '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.branding' => ['nullable', 'array'],
            'settings.branding.*' => ['nullable'],
            'settings.engagement_mode' => ['nullable', Rule::in(['tasks', 'photo_only'])],
        ];
    }

    /**
     * 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'),
        ]);
    }
}
