<?php

namespace App\Http\Requests;

use App\Enums\SpaceOperationalStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'space_type_id' => ['required', 'integer', 'exists:space_types,id'],
            'name' => ['required', 'string', 'max:255'],
            'code' => ['required', 'string', 'max:50', 'unique:spaces,code'],
            'description' => ['nullable', 'string'],
            'location' => ['nullable', 'string', 'max:255'],
            'min_capacity' => ['nullable', 'integer', 'min:0'],
            'max_capacity' => ['nullable', 'integer', 'min:0', 'gte:min_capacity'],
            'standard_rate' => ['nullable', 'numeric', 'min:0'],
            'hourly_rate' => ['nullable', 'numeric', 'min:0'],
            'half_day_rate' => ['nullable', 'numeric', 'min:0'],
            'full_day_rate' => ['nullable', 'numeric', 'min:0'],
            'setup_duration_minutes' => ['nullable', 'integer', 'min:0'],
            'breakdown_duration_minutes' => ['nullable', 'integer', 'min:0'],
            'opening_time' => ['nullable', 'date_format:H:i'],
            'closing_time' => ['nullable', 'date_format:H:i'],
            'amenities' => ['nullable', 'string'],
            'equipment' => ['nullable', 'string'],
            'terms' => ['nullable', 'string'],
            'operational_status' => ['nullable', Rule::enum(SpaceOperationalStatus::class)],
            'layouts' => ['nullable', 'array'],
            'layouts.*.layout_name' => ['required_with:layouts', 'string', 'max:255'],
            'layouts.*.capacity' => ['required_with:layouts', 'integer', 'min:1'],
            'layouts.*.is_custom' => ['nullable', 'boolean'],
            'layouts.*.sort_order' => ['nullable', 'integer', 'min:0'],
        ];
    }
}
