<?php

namespace Functional\Programs\Http\Requests;

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

/**
 * Validation de la mise à jour d'un programme. Les slots, si fournis, remplacent
 * intégralement le planning existant.
 */
class UpdateProgramRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'name' => ['sometimes', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'duration_weeks' => ['sometimes', 'integer', 'min:1'],
            'days_per_week' => ['nullable', 'integer', 'between:1,7'],
            'is_public' => ['sometimes', 'boolean'],
            'slots' => ['sometimes', 'array'],
            'slots.*.week' => ['required', 'integer', 'min:1'],
            'slots.*.weekday' => ['required', 'integer', 'between:1,7'],
            'slots.*.routine_id' => [
                'nullable',
                'integer',
                Rule::exists('routines', 'id')->where(fn ($query) => $query->where('user_id', $this->user()?->id)),
            ],
            'slots.*.label' => ['nullable', 'string', 'max:255'],
        ];
    }
}
