<?php

namespace App\Policies;

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

class ExamPolicy
{
    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-exams');

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



    /**
     * Determine whether the user can manage specific models.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manage(User $user, SemesterExam $model)
    {
        $userPermissions = $user->getUserPermissionByCode('manage-exams');
        $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 exam.'));
    }



    /**
     * 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-exams');
        $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-exams');
        $status = false;

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

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

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





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

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

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

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

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