<?php

namespace App\Policies;

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

class AnnouncementsPolicy
{
    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-announcements');

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

 

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

        $userStages = json_decode($userPermissions['details'], true)['stages'];

        if (in_array($model->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 announcement.'));
    }



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

        $userStages = json_decode($userPermissions['details'], true)['stages'];

        if (in_array(-1, $userStages)) {
            $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 manageStage(User $user, Stage $model)
    {
        $userPermissions = $user->getUserPermissionByCode('manage-announcements');
        $status = false;

        $userStages = json_decode($userPermissions['details'], true)['stages'];

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

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




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

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

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

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

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