<?php

namespace App\Policies;

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

class TicketPolicy
{
    use HandlesAuthorization;

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

    /**
     * Determine whether the user can view the model.
     * Permission + department access check.
     */
    public function view(Admin $admin, Ticket $ticket): bool
    {
        return $admin->hasPermissionTo('view tickets')
            && $ticket->isAccessibleBy($admin);
    }

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

    /**
     * Determine whether the user can update the model.
     */
    public function update(Admin $admin, Ticket $ticket): bool
    {
        return $admin->hasPermissionTo('update tickets')
            && $ticket->isAccessibleBy($admin);
    }

    /**
     * Determine whether the user can update any ticket.
     */
    public function updateAny(Admin $admin): bool
    {
        return $admin->hasPermissionTo('update tickets');
    }

    /**
     * Determine whether the user can delete the model.
     */
    public function delete(Admin $admin, Ticket $ticket): bool
    {
        return $admin->hasPermissionTo('delete tickets')
            && $ticket->isAccessibleBy($admin);
    }

    /**
     * Determine whether the user can delete any ticket.
     */
    public function deleteAny(Admin $admin): bool
    {
        return $admin->hasPermissionTo('delete tickets');
    }

    /**
     * Determine whether the user can reply to tickets.
     */
    public function reply(Admin $admin, Ticket $ticket): bool
    {
        return $admin->hasPermissionTo('reply tickets')
            && $ticket->isAccessibleBy($admin);
    }

    /**
     * Determine whether the user can reply to any ticket.
     */
    public function replyAny(Admin $admin): bool
    {
        return $admin->hasPermissionTo('reply tickets');
    }

    /**
     * PRC-794 — Determine whether the admin can (re)assign the ticket.
     */
    public function assign(Admin $admin, Ticket $ticket): bool
    {
        return $admin->hasPermissionTo('update tickets')
            && $ticket->isAccessibleBy($admin);
    }
}
