<?php

namespace App\Http\Requests;

use App\Enums\BookingStatus;
use App\Enums\ChargeBasis;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateBookingRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->hasPermission('bookings.update') ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'customer_id' => ['sometimes', 'required', 'integer', 'exists:customers,id'],
            'contact_name' => ['nullable', 'string', 'max:255'],
            'contact_phone' => ['nullable', 'string', 'max:50'],
            'contact_email' => ['nullable', 'email', 'max:255'],
            'event_name' => ['sometimes', 'required', 'string', 'max:255'],
            'event_type_id' => ['nullable', 'integer', 'exists:event_types,id'],
            'description' => ['nullable', 'string'],
            'event_start' => ['sometimes', 'required', 'date'],
            'event_end' => ['sometimes', 'required', 'date', 'after:event_start'],
            'guest_count' => ['nullable', 'integer', 'min:0'],
            'seating_arrangement' => ['nullable', 'string', 'max:255'],
            'coordinator_id' => ['nullable', 'integer', 'exists:users,id'],
            'customer_notes' => ['nullable', 'string'],
            'internal_notes' => ['nullable', 'string'],
            'catering_requirements' => ['nullable', 'string'],
            'av_requirements' => ['nullable', 'string'],
            'decorations_requirements' => ['nullable', 'string'],
            'signage_requirements' => ['nullable', 'string'],
            'security_requirements' => ['nullable', 'string'],
            'other_requirements' => ['nullable', 'string'],
            'billable_days_override_reason' => ['nullable', 'string'],
            'billable_hours_override_reason' => ['nullable', 'string'],
            'discount_amount' => ['nullable', 'numeric', 'min:0'],
            'spaces' => ['nullable', 'array', 'min:1'],
            'spaces.*.space_id' => ['required_with:spaces', 'integer', 'exists:spaces,id'],
            'spaces.*.setup_start' => ['nullable', 'date'],
            'spaces.*.event_start' => ['nullable', 'date'],
            'spaces.*.event_end' => ['nullable', 'date'],
            'spaces.*.breakdown_end' => ['nullable', 'date'],
            'spaces.*.seating_layout' => ['nullable', 'string', 'max:255'],
            'spaces.*.guest_count' => ['nullable', 'integer', 'min:0'],
            'spaces.*.unit_price' => ['nullable', 'numeric', 'min:0'],
            'spaces.*.charge_basis' => ['nullable', Rule::enum(ChargeBasis::class)],
            'spaces.*.notes' => ['nullable', 'string'],
            'items' => ['nullable', 'array'],
            'items.*.package_id' => ['nullable', 'integer', 'exists:packages,id'],
            'items.*.product_id' => ['nullable', 'integer', 'exists:products,id'],
            'items.*.description' => ['required_with:items', 'string', 'max:255'],
            'items.*.quantity' => ['nullable', 'numeric', 'min:0'],
            'items.*.unit_price' => ['nullable', 'numeric', 'min:0'],
            'items.*.charge_basis' => ['nullable', Rule::enum(ChargeBasis::class)],
            'items.*.guest_count' => ['nullable', 'integer', 'min:0'],
            'items.*.discount_amount' => ['nullable', 'numeric', 'min:0'],
            'items.*.tax_rate' => ['nullable', 'numeric', 'min:0'],
            'items.*.sort_order' => ['nullable', 'integer', 'min:0'],
            'items.*.is_complimentary' => ['nullable', 'boolean'],
        ];
    }
}
