<?php

namespace App\Http\Requests\Admin\Feedback;

use App\Enums\FeedbackStatus;
use App\Enums\FeedbackType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class FeedbackRequest extends FormRequest
{
    public function authorize(): bool
    {
        if ($this->isMethod('post')) {
            return $this->user()->can('create-feedback');
        }

        $feedback = $this->route('feedback');

        return $this->user()->can('update-feedback')
            && $feedback->user_id === $this->user()->id
            && $feedback->status === FeedbackStatus::Submitted;
    }

    public function rules(): array
    {
        return [
            'type' => ['required', 'string', Rule::enum(FeedbackType::class)],
            'subject' => ['required', 'string', 'max:150'],
            'message' => ['required', 'string'],
        ];
    }
}
