<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name', 'email', 'password', 'phone', 'company_id', 'role'
    ];

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

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

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

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

    /**
     * Company relationship.
     */
    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    /**
     * Subscriptions relationship.
     */
    public function subscriptions()
    {
        return $this->hasMany(Subscription::class);
    }

    /**
     * Latest subscription relationship.
     */
    public function subscription()
    {
        return $this->hasOne(Subscription::class)->latestOfMany();
    }

    /**
     * Get active subscription.
     */
    public function getActiveSubscription()
    {
        return $this->subscriptions()
            ->where('status', 'active')
            ->where('payment_status', 'completed')
            ->latest()
            ->first();
    }

    /**
     * Get driver limit from active subscription.
     */
    public function getDriverLimit()
    {
        return $this->getActiveSubscription()?->package->driver_limit ?? 0;
    }

    /**
     * Get truck limit from active subscription.
     */
    public function getTruckLimit()
    {
        return $this->getActiveSubscription()?->package->truck_limit ?? 0;
    }

    /**
     * Scope to current user's company.
     */
    public function scopeCompany($query)
    {
$companyId = auth()->check() ? auth()->user()?->company_id : null;
        return $query->where('company_id', $companyId);
    }

    /**
     * Check if user is owner.
     */
    public function isOwner()
    {
        return $this->role === 'owner';
    }

    /**
     * Check if user is admin.
     */
    public function isAdmin()
    {
        return $this->role === 'admin';
    }
}
