<?php

namespace App\Policies;

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

class DivisionPolicy
{
    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-school-structure');

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

    /**
     * Determine whether the user can manage customised model.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manage(User $user, Division $model)
    {
        $userPermission = $user->getUserPermissionByCode('manage-school-structure');
        $status = false;

        if ($userPermission) {
            $level = Level::find($model->level_id);
            $userStages = json_decode($userPermission['details'], true)['stages'];
            if (in_array($level->stage_id, $userStages) || in_array(-1, $userStages)) {
                $status = true;
            }
        }

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

    

    /**
     * Determine whether the user can manage models in specific stage.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function manageStage(User $user, Stage $stage)
    {
        $userPermissions = $user->getUserPermissionByCode('manage-school-structure');
        $status = false;
        
        $userStages = json_decode($userPermissions['details'], true)['stages'];

        if (in_array($stage->id , $userStages) || in_array(-1, $userStages) ) {
            $status = true;
        }
        
        return $status
            ? Response::allow()
            : Response::deny(trans('general.You do not have permission to access study materials in this stage.'));
    }

    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function viewAny(User $user)
    {
        return true;
        return $user->hasPermission('browse-divisions')
            ? Response::allow()
            : Response::deny(trans('general.You do not have permission to browse divisions.'));
    }

    /**
     * Determine whether the user can view the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\User  $model
     * @return mixed
     */
    public function view(User $user)
    {
        return true;
        return $user->hasPermission('view-divisions')
            ? Response::allow()
            : Response::deny('You do not have permission to read divisions information.');
    }

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\User  $user
     * @return mixed
     */
    public function create(User $user)
    {
        return true;
        return $user->hasPermission('add-divisions')
            ? Response::allow()
            : Response::deny('You do not have permission to add new divisions.');
    }

    /**
     * Determine whether the user can update the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\User  $model
     * @return mixed
     */
    public function update(User $user)
    {
        return true;
        return $user->hasPermission('edit-divisions')
            ? Response::allow()
            : Response::deny('You do not have permission to edit divisions informations.');
    }


    /**
     * Determine whether the user can delete the model.
     *
     * @param  \App\Models\User  $user
     * @param  \App\Models\Role  $role
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function delete(User $user)
    {
        return true;
        return $user->hasPermission('delete-divisions')
            ? Response::allow()
            : Response::deny('You do not have permission to delete divisions.');
    }

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

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