<?php

namespace App\Policies;

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

class StudentsPolicy
{
    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-students');

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



    /**
     * Determine whether the user can manage specific models.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manage(User $user, Student $model)
    {
        $userPermissions = $user->getUserPermissionByCode('manage-students');
        $status = false;

        $userDivisions = json_decode($userPermissions['details'], true)['divisions'];

        if (in_array($model->division_id, $userDivisions) || in_array(-1, $userDivisions)) {
            $status = true;
        }

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



    /**
     * 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-students');
        $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 super permission.'));
    }


    /**
     * Determine whether the user can manage specific models.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manageDivision(User $user, Division $model)
    {
        $userPermissions = $user->getUserPermissionByCode('manage-students');
        $status = false;

        $userDivisions = json_decode($userPermissions['details'], true)['divisions'];

        if (in_array($model->id, $userDivisions) > 0 || in_array(-1, $userDivisions)) {
            $status = true;
        }

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




    /**
     * 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\Student  $student
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function view(ParentAccount $parentAccount, Student $student)
    {
        //
    }

    /**
     * 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\Student  $student
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function update(ParentAccount $parentAccount, Student $student)
    {
        //
    }

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

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

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