<?php

namespace App\Http\Requests;

use App\Models\Task;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class TaskUpdateRequest extends FormRequest
{
    public function authorize(): bool
    {
        $task = $this->route('task') ?? Task::find($this->route('task'));
        if ($task) {
            return $this->user()->can('update', $task);
        }
        return $this->user()->can('update tasks');
    }

    public function rules(): array
    {
        return [
            'title' => 'sometimes|required|string|max:255',
            'description' => 'nullable|string',
            'phase_id' => 'nullable|exists:phases,id',
            'status' => ['sometimes', 'required', Rule::in(['pending', 'in_progress', 'completed', 'cancelled'])],
            'priority' => ['sometimes', 'required', Rule::in(['low', 'medium', 'high', 'critical'])],
            'assigned_to' => 'nullable|exists:users,id',
            'due_date' => 'nullable|date',
            'start_date' => 'nullable|date|before_or_equal:due_date',
            'estimated_hours' => 'nullable|integer|min:0|max:10000',
            'actual_hours' => 'nullable|integer|min:0|max:10000',
            'order' => 'nullable|integer|min:0',
            'parent_task_id' => 'nullable|exists:tasks,id',
        ];
    }
}