<?php

namespace App\Policies;

use App\Models\Parents;
use App\Models\ParentAccount;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;

class ParentsPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can manage any models.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manageAny(User $user)
    {
        $userPermission = $user->getUserPermissionByCode('manage-parents');

        return $userPermission
            ? Response::allow()
            : Response::deny(trans('general.You do not have permission to manage Parents.'));
    }



    /**
     * Determine whether the user can manage specific models.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manage(User $user, Parents $model)
    {
        $userPermissions = $user->getUserPermissionByCode('manage-parents');
        $status = false;
        $parentsDivisions = $model->getParentsDivisions();
        $userDivisions = json_decode($userPermissions['details'], true)['divisions'];

        if (count(array_intersect($parentsDivisions , $userDivisions)) > 0 || in_array(-1, $userDivisions) ) {
            $status = true;
        }

        return $status
            ? Response::allow()
            : Response::deny(trans('general.You do not have permission to manage this parent.'));
    }


    /**
     * Determine whether the user can manage models as Super Admin.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manageAsSuper(User $user)
    {
        $userPermissions = $user->getUserPermissionByCode('manage-parents');
        $status = false;
        
        $userDivisions = json_decode($userPermissions['details'], true)['divisions'];

        if (in_array(-1, $userDivisions) ) {
            $status = true;
        }

        return $status
            ? Response::allow()
            : Response::deny(trans('general.You do not have permission to manage users as Super.'));
    }




    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\Models\ParentAccount  $parentAccount
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function viewAny(ParentAccount $parentAccount)
    {
        //
    }

    /**
     * Determine whether the user can view the model.
     *
     * @param  \App\Models\ParentAccount  $parentAccount
     * @param  \App\Models\Parents  $parent
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function view(ParentAccount $parentAccount, Parents $parent)
    {
        //
    }

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\ParentAccount  $parentAccount
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function create(ParentAccount $parentAccount)
    {
        //
    }

    /**
     * Determine whether the user can update the model.
     *
     * @param  \App\Models\ParentAccount  $parentAccount
     * @param  \App\Models\Parents  $parent
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function update(ParentAccount $parentAccount, Parents $parent)
    {
        //
    }

    /**
     * Determine whether the user can delete the model.
     *
     * @param  \App\Models\ParentAccount  $parentAccount
     * @param  \App\Models\Parents  $parent
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function delete(ParentAccount $parentAccount, Parents $parent)
    {
        //
    }

    /**
     * Determine whether the user can restore the model.
     *
     * @param  \App\Models\ParentAccount  $parentAccount
     * @param  \App\Models\Parents  $parent
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function restore(ParentAccount $parentAccount, Parents $parent)
    {
        //
    }

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