<?php

namespace App\Http\Requests;

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

class TaskStoreRequest extends FormRequest
{
    public function authorize(): bool
    {
        $project = $this->route('project') ?? $this->input('project_id');
        if ($project instanceof Project) {
            return $this->user()->can('create tasks', $project);
        }
        return $this->user()->can('create tasks');
    }

    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'phase_id' => 'nullable|exists:phases,id',
            'status' => ['required', Rule::in(['pending', 'in_progress', 'completed', 'cancelled'])],
            'priority' => ['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',
            'project_id' => 'required|exists:projects,id',
        ];
    }
}