<?php

namespace App\Policies;

use App\Models\Campaign;
use App\Models\Organization;
use App\Models\User;

class CampaignPolicy extends BasePolicy
{
    public function viewAny(User $user, ?Organization $organization = null): bool
    {
        return $this->belongsToOrganization($user, $organization);
    }

    public function view(User $user, Campaign $campaign, ?Organization $organization = null): bool
    {
        if (!$this->belongsToOrganization($user, $organization)) {
            return false;
        }

        return $this->resourceBelongsToOrganization($campaign, $organization, $user);
    }

    public function create(User $user, ?Organization $organization = null): bool
    {
        return $this->belongsToOrganization($user, $organization);
    }

    public function update(User $user, Campaign $campaign, ?Organization $organization = null): bool
    {
        if (!$this->belongsToOrganization($user, $organization)) {
            return false;
        }

        return $this->resourceBelongsToOrganization($campaign, $organization, $user);
    }

    public function delete(User $user, Campaign $campaign, ?Organization $organization = null): bool
    {
        if (!$this->belongsToOrganization($user, $organization)) {
            return false;
        }

        return $this->resourceBelongsToOrganization($campaign, $organization, $user);
    }
}
