<?php

namespace App\Http\Requests;

use App\Repositories\CampaignRepository;
use App\Support\IdFormatter;

class CampaignUpdateRequest extends CampaignRequest
{
    public function authorize(): bool
    {
        $organization = $this->attributes->get('current_organization');
        $campaignId = $this->route('id');
        $orgHex = IdFormatter::formatModelId($organization);

        $campaign = (new CampaignRepository())->getByIdInOrganization($campaignId, $orgHex);

        if (!$campaign) {
            return false;
        }

        return $this->user()->can('update', [$campaign, $organization]);
    }
}
