<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

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

    protected $fillable = [
        'name',
        'email',
        'phone',
        'role',
        'unique_token',
        'profile_image',
        'fcm_token',
        'status',
        'is_first_login',
        'password',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'phone_verified_at' => 'datetime',
            'password' => 'hashed',
            'status' => 'boolean',
            'is_first_login' => 'boolean',
        ];
    }

    public function vendorProfile()
    {
        return $this->hasOne(VendorProfile::class);
    }

    public function subscriptions()
    {
        return $this->hasMany(Subscription::class);
    }

    public function activeSubscription()
    {
        return $this->hasOne(Subscription::class)->where('status', 'active')->where('expires_at', '>', now())->latestOfMany();
    }

    public function frames()
    {
        return $this->hasMany(Frame::class, 'vendor_id');
    }

    public function lenses()
    {
        return $this->hasMany(Lens::class, 'vendor_id');
    }

    public function banners()
    {
        return $this->hasMany(VendorBanner::class, 'vendor_id');
    }

    public function offers()
    {
        return $this->hasMany(VendorOffer::class, 'vendor_id');
    }

    public function events()
    {
        return $this->hasMany(VendorEvent::class, 'vendor_id');
    }

    public function vendorOrders()
    {
        return $this->hasMany(Order::class, 'vendor_id');
    }

    public function customerOrders()
    {
        return $this->hasMany(Order::class, 'customer_id');
    }

    public function sentNotifications()
    {
        return $this->hasMany(AppNotification::class, 'sender_id');
    }

    public function isSuperAdmin(): bool
    {
        return $this->role === 'superadmin';
    }

    public function isVendor(): bool
    {
        return $this->role === 'vendor';
    }

    public function isCustomer(): bool
    {
        return $this->role === 'customer';
    }
}
