<?php

namespace App\Models;

use App\Enum\UserType;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable;

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'user_type' => UserType::class,
        'email_verified_at' => 'datetime',
        'phone_verified_at' => 'datetime',
        'otp_expiry_at' => 'datetime',
        'invalid_pin_tries_at' => 'datetime',
        'email_ver' => 'boolean',
        'phone_ver' => 'boolean',
        'otp_ver' => 'boolean',
    ];

    /**
     * Attributes that are mass assignable.
     */
    protected $fillable = [
        'firstname',
        'lastname',
        'email',
        'phone',
        'password',
        'state',
        'status',
        'user_type',
        'otp',
        'otp_reason',
        'otp_expiry_at',
        'email_ver',
        'email_ver_at',
        'phone_ver',
        'phone_ver_at',
        'otp_ver_at',
        'invalid_pin_tries_at',
    ];

    /**
     * Attributes that should be hidden for arrays or JSON output.
     */
    protected $hidden = [
        'password',
        'remember_token',
        'invalid_pin_tries',
        'invalid_pin_tries_at',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($user) {
            if (empty($user->id)) {
                $user->id = Str::uuid();
            }
            if (empty($user->track_id)) {
                $user->user_id = 'USR_' . strtoupper(Str::random(6));
            }
        });
    }

    public function getJWTIdentifier()
    {
        return $this->getKey(); // Usually the primary key (id)
    }

    public function getJWTCustomClaims()
    {
        return []; // You can return extra claims if needed
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    public function admin()
    {
        return $this->hasOne(Admin::class);
    }

    public function vendor()
    {
        return $this->hasOne(Vendor::class);
    }

    /**
     * Check if user is admin
     */
    public function isAdmin()
    {
        return $this->user_type === UserType::ADMIN;
    }

    /**
     * Check if user is vendor
     */
    public function isVendor()
    {
        return $this->user_type === UserType::VENDOR;
    }

    /**
     * Check if user is customer
     */
    public function isCustomer()
    {
        return $this->user_type === UserType::CUSTOMER;
    }

    /**
     * Get the user's full name
     */
    public function getFullNameAttribute()
    {
        return "{$this->firstname} {$this->lastname}";
    }

    /**
     * Get the user's name (alias for full name)
     */
    public function getNameAttribute()
    {
        return $this->getFullNameAttribute();
    }
}
