<?php

namespace App\Http\Requests;

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

class IssueUpdateRequest extends FormRequest
{
    public function authorize(): bool
    {
        $issue = $this->route('issue') ?? Issue::find($this->route('issue'));
        if ($issue) {
            return $this->user()->can('edit issues', $issue->project) && $this->user()->can('edit', $issue);
        }
        return $this->user()->can('edit issues');
    }

    public function rules(): array
    {
        return [
            'title' => 'sometimes|required|string|max:255',
            'description' => 'nullable|string',
            'status' => ['sometimes', 'required', Rule::in(Issue::STATUSES)],
            'priority' => ['sometimes', 'required', Rule::in(Issue::PRIORITIES)],
            'type' => ['sometimes', 'required', Rule::in(Issue::TYPES)],
            'assigned_to' => 'nullable|exists:users,id',
            'resolution_notes' => 'nullable|string',
            'feature_id' => 'nullable|exists:features,id',
            'inspection_id' => 'nullable|exists:inspections,id',
        ];
    }
}