<?php

namespace App\Http\Requests;

use App\Models\CallbackRequest;
use Illuminate\Foundation\Http\FormRequest;

class CompleteCallbackRequestRequest extends FormRequest
{
    public function authorize(): bool
    {
        $callbackRequest = $this->route('callbackRequest');
        $membership = $callbackRequest instanceof CallbackRequest
            ? $this->user()?->memberships()
                ->where('organization_id', $callbackRequest->organization_id)
                ->first()
            : null;

        return $membership !== null
            && ($membership->role === 'owner' || $membership->hasPrivilege('manage_callbacks'));
    }

    /**
     * @return array<string, list<string>>
     */
    public function rules(): array
    {
        return [
            'notes' => ['nullable', 'string', 'max:2000'],
        ];
    }
}
