<?php // Code within app\Helpers\Helper.php
namespace App\Helpers;

use App\Jobs\SendEmailJob;
use App\Models\CityList;
use App\Models\Country;
use App\Models\School;
use App\Models\StateList;
use App\Models\Subject;
use Session;
use App\Models\User;
use Config;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class Helper
{

    public static function successMsg($name, $msg)
    {
        if ($name == 'insert') {
            self::SetMessage('message', $msg . ' Inserted Successfully !');
        } elseif ($name == 'update') {
            self::SetMessage('message', $msg . ' Updated Successfully !');
        } elseif ($name == 'delete') {
            self::SetMessage('message', $msg . ' Deleted Successfully !');
        } elseif ($name == 'custom') {
            self::SetMessage('message', $msg);
        }
    }

    public static function activeDeactiveMsg($name, $msg)
    {
        if ($name == 'active') {
            self::SetMessage('message', $msg . ' Active Successfully !');
        } else {
            self::SetMessage('message', $msg . ' Deactive Successfully !');
        }
    }

    public static function failarMsg($name, $msg)
    {
        if ($name == 'custom') {
            self::SetMessage('failmessage', $msg);
        }
    }

    public static function SetMessage($type ,$message){ 
        Session::flash($type,$message); 
    }
    public static function ForgetMessage(){ 
        // Session::forget('message');
        // // Session::forget('user_message_type');
        // Session::save();
    }
    //User Role has permission
    public static function roleHasPermission($name = "", $user_id = "", $branch_id = "")
    {
        $id = Auth::user()->id;
        $data = User::with("roles")->find($id);
        $role_id = $data->roles->pluck('id')->first();
        $role_name = $data->roles->pluck('name')->first();
        
        if($role_name == "Super Admin"){
            return true;
        }
        $permissions_id = Permission::where("name",$name)->pluck('id')->first();
        // dd($permissions_id);
        $where = array("permission_id" => $permissions_id, "role_id" => $role_id);
        if($user_id != ""){
            $where['user_id'] = $user_id;
        }
        if($branch_id != ""){
            $where['branch_id'] = $branch_id;
        }
        $roleHasPermission = DB::table('role_has_permissions')
                ->where($where)
                // ->whereDate("start_date", "<=", date("Y-m-d"))
                // ->whereDate("end_date", ">=", date("Y-m-d"))
                ->first();
        if(!empty($roleHasPermission)){
            return true;
        }else{
            return false;
        }
       
    }

     //Country 
     public static function country($id){
       
        $where = array(
            ["country_id", "=", $id],
        );
       
        $data = Country::select("country_id as id","country_name as name")->where($where)->first();
        return $data;
    }
    //State 
    public static function state($id){
       
        $where = array(
            ["state_id", "=", $id],
        );
       
        $data = StateList::select("state_id as id","state_name as name")->where($where)->first();
        return $data;
    }
    //City 
    public static function city($id){
       
        $where = array(
            ["city_id", "=", $id],
        );
       
        $data = CityList::select("city_id as id","city_name as name")->where($where)->first();
        return $data;
    }
    //School 
    public static function school($id){
       
        $where = array(
            ["id", "=", $id],
        );
       
        $data = School::select("id","name")->where($where)->first();
        return $data;
    }
    //Subject 
    public static function subject($id){
       
        $where = array(
            ["id", "=", $id],
        );
       
        $data = Subject::select("id","name")->where($where)->first();
        return $data;
    }
   
    //Day name 
    public static function dayName($id){
        $data = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
        return $data;
    }

   
}
