<?php

namespace App\Policies;

use App\Models\User;
use App\Models\Affiliate;
use App\Classes\ValueObjects\Constants\RoleTypes;
use Illuminate\Auth\Access\HandlesAuthorization;

class AffiliatePolicy
{
    use HandlesAuthorization;

    /**
     * Check if user is admin
     *
     * @param  User  $user
     * @return bool
     */
    private function isAdmin(User $user): bool
    {
        return in_array($user->type, RoleTypes::ADMIN_ROLES);
    }

    /**
     * Determine whether the user can view any affiliates.
     *
     * @param  User  $user
     * @return bool
     */
    public function viewAny(User $user)
    {
        // Only admins can view affiliates list
        return $this->isAdmin($user);
    }

    /**
     * Determine whether the user can view the affiliate.
     *
     * @param  User  $user
     * @param  Affiliate  $affiliate
     * @return bool
     */
    public function view(User $user, Affiliate $affiliate)
    {
        return $this->isAdmin($user);
    }

    /**
     * Determine whether the user can create affiliates.
     *
     * @param  User  $user
     * @return bool
     */
    public function create(User $user)
    {
        return $this->isAdmin($user);
    }

    /**
     * Determine whether the user can update the affiliate.
     *
     * @param  User  $user
     * @param  Affiliate  $affiliate
     * @return bool
     */
    public function update(User $user, Affiliate $affiliate)
    {
        return $this->isAdmin($user);
    }

    /**
     * Determine whether the user can delete the affiliate.
     *
     * @param  User  $user
     * @param  Affiliate  $affiliate
     * @return bool
     */
    public function delete(User $user, Affiliate $affiliate)
    {
        return $this->isAdmin($user);
    }

    /**
     * Determine whether the user can manage affiliate settings.
     *
     * @param  User  $user
     * @return bool
     */
    public function manageSettings(User $user)
    {
        return $this->isAdmin($user);
    }
}


