<?php

namespace App\Models;

use Carbon\Carbon;
use App\Models\Traits\GeneratesUserId;
use App\Models\Traits\HasEmployeeUser;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Modules\Auth\Notifications\ResetPasswordLink;
use Modules\Core\Enums\RolesEnum;
use Modules\Core\Traits\HasStatus;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements JWTSubject, MustVerifyEmail
{
    use HasFactory,
        Notifiable,
        HasRoles,
        GeneratesUserId,
        HasEmployeeUser,
        HasStatus,
        SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'userId',
        'name',
        'email',
        'password',
        'password_changed_at',
        'phone',
        'image',
        'sign',
        'status',
        'email_verified_at'
    ];

    protected $guard_name = 'api';

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<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',
            'password_changed_at' => 'datetime',
            'deleted_at' => 'datetime',
        ];
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier(): mixed
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims(): array
    {
        return [
            'password_changed_at' => $this->password_changed_at ? Carbon::parse($this->password_changed_at)->timestamp : null,
        ];
    }

    public function sendEmailVerificationNotification()
    {
        //$this->notify(new RegisterVerifyEmail());
    }

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordLink($token, $this->email));
    }

    public function scopeExcludeSuperAdmin($query)
    {
        return $query->whereDoesntHave('roles', function ($q) {
            $q->where('name', RolesEnum::SUPER_ADMIN->value);
        });
    }

    public function scopeFilterId($query, $id)
    {
        return $id ? $query->where('id', $id) : $query;
    }

    public function scopeSearch($query, ?string $search)
    {
        if (blank($search)) {
            return $query;
        }

        $like = "%{$search}%";

        return $query->where(function ($q) use ($like) {
            $q->where('name', 'LIKE', $like)
                ->orWhere('userId', 'LIKE', $like)
                ->orWhere('email', 'LIKE', $like)
                ->orWhere('phone', 'LIKE', $like);
        });
    }
}
