<?php

namespace App\Policies;

use App\Models\Agency\AgencyPage;
use App\Models\User;

class AgencyPagePolicy
{
    /**
     * Determine if user can view the page
     */
    public function view(User $user, AgencyPage $page): bool
    {
        return $user->currentAgencyId() === $page->agency_id;
    }

    /**
     * Determine if user can update the page
     */
    public function update(User $user, AgencyPage $page): bool
    {
        // Agency owner or manager
        return $user->currentAgencyId() === $page->agency_id
            && ($user->isAgencyOwner() || $user->hasRole(['agency-manager']));
    }

    /**
     * Determine if user can delete the page
     */
    public function delete(User $user, AgencyPage $page): bool
    {
        return $user->currentAgencyId() === $page->agency_id
            && $user->isAgencyOwner();
    }

    /**
     * Determine if user can publish the page
     */
    public function publish(User $user, AgencyPage $page): bool
    {
        return $user->currentAgencyId() === $page->agency_id
            && ($user->isAgencyOwner() || $user->hasRole(['agency-manager']));
    }

    /**
     * Determine if user can manage revisions
     */
    public function manageRevisions(User $user, AgencyPage $page): bool
    {
        return $user->currentAgencyId() === $page->agency_id
            && ($user->isAgencyOwner() || $user->hasRole(['agency-manager']));
    }
}
