<?php

namespace App\Policies;

use App\Models\StaffProfile;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class StaffProfilePolicy
{
    use HandlesAuthorization;

    /**
     * If we are admin return true right away
     */
    public function before($user, $ability)
    {
        if ($user->hasRole('admin')) {
            return true;
        }
    }

    /**
     * Determine whether the user can manage staff profiles
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manage(User $user)
    {
        return $user->hasRole('staff-profiles-manager');
    }


    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function viewAny(User $user)
    {
        return $user->hasRole('staff-profiles-manager');
    }

    /**
     * Determine whether the user can view the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\StaffProfile  $staffProfile
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function view(User $user, StaffProfile $staffProfile)
    {
        return $user->canPerformAction('view', $staffProfile);
    }

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function create(User $user)
    {
        return $user->hasRole('staff-profiles-manager');
    }

    /**
     * Determine whether the user can update the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\StaffProfile  $staffProfile
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function update(User $user, StaffProfile $staffProfile)
    {
        if ($staffProfile->user_id === $user->id) {
            return true;
        }

        if ($user->hasRole('staff-profiles-manager')) {
            return true;
        }

        return $user->canPerformAction('update', $staffProfile);
    }

    /**
     * Determine whether the user can delete the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\StaffProfile  $staffProfile
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function delete(User $user, StaffProfile $staffProfile)
    {
        return $user->hasRole('staff-profiles-manager');
    }

    /**
     * Determine whether the user can restore the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\StaffProfile  $staffProfile
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function restore(User $user, StaffProfile $staffProfile)
    {
        return $user->hasRole('staff-profiles-manager');
    }

    /**
     * Determine whether the user can permanently delete the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\StaffProfile  $staffProfile
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function forceDelete(User $user, StaffProfile $staffProfile)
    {
        //
    }

    /**
     * Determine whether the user can publish any models.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\StaffProfile  $staffProfile
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function publish(User $user, StaffProfile $staffProfile)
    {
        return $user->hasRole('staff-profiles-manager');
    }
}
