<?php

namespace App\Policies;

use App\Models\Admin;
use App\Models\ProductConfigOption;
use Illuminate\Auth\Access\HandlesAuthorization;

class ProductConfigOptionPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the admin can view any models.
     */
    public function viewAny(Admin $admin): bool
    {
        return $admin->hasPermissionTo('view product config options');
    }

    /**
     * Determine whether the admin can view the model.
     */
    public function view(Admin $admin, ProductConfigOption $productConfigOption): bool
    {
        return $admin->hasPermissionTo('view product config options');
    }

    /**
     * Determine whether the admin can create models.
     */
    public function create(Admin $admin): bool
    {
        return $admin->hasPermissionTo('create product config options');
    }

    /**
     * Determine whether the admin can update the model.
     */
    public function update(Admin $admin, ProductConfigOption $productConfigOption): bool
    {
        return $admin->hasPermissionTo('update product config options');
    }

    /**
     * Determine whether the admin can update any model.
     */
    public function updateAny(Admin $admin): bool
    {
        return $admin->hasPermissionTo('update product config options');
    }

    /**
     * Determine whether the admin can delete the model.
     */
    public function delete(Admin $admin, ProductConfigOption $productConfigOption): bool
    {
        return $admin->hasPermissionTo('delete product config options');
    }

    /**
     * Determine whether the admin can delete any model.
     */
    public function deleteAny(Admin $admin): bool
    {
        return $admin->hasPermissionTo('delete product config options');
    }

    /**
     * Determine whether the admin can archive the option.
     */
    public function archive(Admin $admin, ProductConfigOption $option): bool
    {
        return $admin->hasPermissionTo('delete product config options')
            && ! $option->isArchived();
    }

    /**
     * Determine whether the admin can restore the archived option.
     */
    public function restore(Admin $admin, ProductConfigOption $option): bool
    {
        return $admin->hasPermissionTo('restore product config options')
            && $option->isArchived();
    }

    /**
     * Determine whether the admin can permanently delete the option.
     */
    public function forceDelete(Admin $admin, ProductConfigOption $option): bool
    {
        return $admin->hasPermissionTo('force delete product config options')
            && $option->isArchived()
            && ! $option->products()->exists()
            && ! $option->configOptionValues()->exists();
    }
}
