<?php

namespace App\Http\Requests;

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

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

    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'status' => ['required', Rule::in(Issue::STATUSES)],
            'priority' => ['required', Rule::in(Issue::PRIORITIES)],
            'type' => ['required', Rule::in(Issue::TYPES)],
            'assignedTo' => 'nullable|exists:users,id',
            'resolutionNotes' => 'nullable|string',
            'featureId' => 'nullable|exists:features,id',
            'inspectionId' => 'nullable|exists:inspections,id',
            'project_id' => 'required|exists:projects,id',
        ];
    }
}