<?php

namespace App\Http\Requests;

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

class AuthorisedPartnerRequest extends FormRequest
{
    use PartnerAccountRules;

    public function authorize(): bool
    {
        $partner = $this->route('authorised_partner');

        return $partner
            ? $this->user()->can('update', $partner)
            : $this->user()->can('create', AuthorisedPartner::class);
    }

    public function rules(): array
    {
        $ignoreUserId = $this->route('authorised_partner')?->user_id;

        return array_merge($this->accountRules($ignoreUserId), $this->officialRules(), [
            'promoter_id' => ['nullable', 'integer', 'exists:promoters,id'],
        ]);
    }
}
