<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\admin\ManageFeedback;
use App\Models\ManageModuleLink;
use App\Models\ManageModule;
use App\Models\OrderedPassport;


class IamPrincipal extends Authenticatable implements JWTSubject
{
    use SoftDeletes;
    use HasApiTokens, HasFactory, Notifiable;

    protected $table = 'iam_principal';
    // protected $dates = ['deleted_at'];

    protected $fillable = [
        'one_signal_player_id',
        'first_name',
        'last_name',
        'email_address',
        'password',
        'principal_type_xid',
        'principal_source_xid',
        'date_of_birth',
        'phone_number',
        'profile_photo',
        'address_line1',
        'user_name',
        'state_xid',
        'is_active',
        'notification_status',
        'deleted_by_admin',
        'referral_code'
    ];

    public function moduleLinks()
    {
        return $this->hasMany(ManageModuleLink::class, 'principal_xid', 'id');
    }

    public function feedbacks()
    {
        return $this->hasMany(ManageFeedback::class, 'principal_xid', 'id');
    }
    public function getresturant()
    {
        return $this->hasMany(IamPrincipalRestaurantRole::class, 'principal_xid', 'id');
    }

    public function getCustomerCount()
    {
        // Fetch the count of customers
        $customerCount = IamPrincipal::where('principal_type_xid', '=', 3)->count();

        return $customerCount;
    }



    // protected $fillable =
    // [
    //     'principal_type_xid',
    //     'principal_source_xid',
    //     'user_name',
    // 'password_hash',
    //     'pin',
    // 'first_name',
    //     'last_name',
    //     'gender',
    //     'date_of_birth',
    //     'phone_number',
    //     'other_phone_number',
    // 'email_address',
    //     'address_line1',
    //     'address_line2',
    //     'city_xid',
    //     'state_xid',
    //     'country_xid',
    //     'post_code',
    //     'last_login_datetime',
    //     'profile_photo',
    //     'referral_code',
    //     'description',
    //     'is_active'
    // ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */


    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];

    public function getPermissionGranted($id, $module)
    {
        //        $id is used as authuser id
        //         $moudle is the slug of sidebar module

        $isSubAdmin = IamPrincipal::where('id', $id)->where('principal_type_xid', 2)->first();
        //         'is_admin',1 is for checking the login user is subadmin or not

        $isMainAdmin = IamPrincipal::where('id', $id)->where('principal_type_xid', 1)->first();
        if ($isMainAdmin) {
            return true;
        } elseif ($isSubAdmin) {
            //search for module
            $isModule = ManageModule::where('slug', $module)->first();
            if ($isModule) {
                $isSubAdminModuleLink = ManageModuleLink::where('principal_xid', $id)
                    ->where('manage_modules_xid', $isModule->id)->first();
                //         dd($id,$module,$isSubAdmin->id,$isModule,$isSubAdminModuleLink);
                if ($isSubAdminModuleLink) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    // public function orders()
    // {
    //     return $this->hasMany(OrderedPassport::class, 'iam_principal_xid','id');
    // }

    // public function notification()
    // {
    //     return $this->hasMany(NotificationDetails::class,'principal_xid', 'id');
    // }
    public function state()
    {
        return $this->belongsTo(ManageState::class, 'state_xid', 'id');
    }
    public function roles()
    {
        return $this->hasMany(IamPrincipalRestaurantRole::class, 'id', 'principal_xid');
    }
    // public function restaurants()
    // {
    //     return $this->hasMany(ManageRestaurant::class, 'restaurant_id', 'id');
    // }

    public function restaurant()
    {
        return $this->hasMany(ManageRestaurant::class, 'id', 'restaurant_xid');
    }
    public function contactMessages()
    {
        return $this->hasMany(ManageContactus::class, 'principal_xid', 'id');
    }
}
