<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Models\Permission;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'username',
        'name',
        'email',
        'google_id',
        'avatar',
        'status',
        'password',
    ];

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

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    public function getAvatarUrlAttribute(): ?string
    {
        if (!$this->avatar) return null;
        if (str_starts_with($this->avatar, 'http')) return $this->avatar;
        return asset($this->avatar);
    }

    // Roles and Permissons
    public static function getPermissionGroups()
    {
        $permissionGroups = Permission::select('group_name as name')
            ->groupBy('group_name')
            ->orderBy('id', 'asc')
            ->get();

        return $permissionGroups;
    }

    public static function getPermissionsByGroupName($groupName)
    {
        $permissions = Permission::select('name', 'id')
            ->where('group_name', $groupName)
            ->get();

        return $permissions;
    }

    public static function roleHasAllPermissions($role, $permissions)
    {
        $hasAllPermissions = true;

        foreach ($permissions as $permission) {
            if (!$role->hasPermissionTo($permission->name)) {
                $hasAllPermissions = false;
                return $hasAllPermissions;
            }
        }

        return $hasAllPermissions;
    }

    // Relations
    public function country()
    {
        return $this->belongsTo(Country::class);
    }

    public function state()
    {
        return $this->belongsTo(State::class);
    }

    public function district()
    {
        return $this->belongsTo(District::class);
    }

    public function subDistrict()
    {
        return $this->belongsTo(SubDistrict::class);
    }

    public function village()
    {
        return $this->belongsTo(Village::class);
    }

    public function profileChangeRequest()
    {
        return $this->hasOne(ProfileChangeRequest::class);
    }

    public function profileChangeRequests()
    {
        return $this->hasMany(ProfileChangeRequest::class); 
    }

    public function referralSource()
    {
        return $this->belongsTo(ReferralSource::class);
    }
    
}
