<?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;

#[Fillable([
    'uuid',
    'public_id',
    'referral_code',
    'referred_by_user_id',
    'referred_at',
    'name',
    'display_name',
    'email',
    'phone',
    'country_id',
    'currency_id',
    'locale',
    'timezone',
    'status',
    'role',
    'risk_score',
    'loyalty_points',
    'password',
])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, Notifiable;

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

    public function wallet()
    {
        return $this->hasOne(Wallet::class);
    }

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

    public function customerNotifications()
    {
        return $this->hasMany(CustomerNotification::class);
    }

    public function couponRedemptions()
    {
        return $this->hasMany(CouponRedemption::class);
    }

    public function referrer()
    {
        return $this->belongsTo(User::class, 'referred_by_user_id');
    }

    public function referredUsers()
    {
        return $this->hasMany(User::class, 'referred_by_user_id');
    }

    public function referralRewardsSent()
    {
        return $this->hasMany(ReferralReward::class, 'referrer_user_id');
    }

    public function referralRewardsReceived()
    {
        return $this->hasMany(ReferralReward::class, 'referred_user_id');
    }

    public function sentWalletTransfers()
    {
        return $this->hasMany(WalletTransfer::class, 'sender_user_id');
    }

    public function receivedWalletTransfers()
    {
        return $this->hasMany(WalletTransfer::class, 'receiver_user_id');
    }

    public function voucherRedemptions()
    {
        return $this->hasMany(VoucherRedemption::class);
    }

    public function supportTickets()
    {
        return $this->hasMany(SupportTicket::class);
    }

    public function favorites()
    {
        return $this->hasMany(Favorite::class);
    }

    public function favoriteGames()
    {
        return $this->belongsToMany(Game::class, 'favorites')->withTimestamps();
    }

    public function manualPaymentSubmissions()
    {
        return $this->hasMany(ManualPaymentSubmission::class);
    }

    public function adminNotes()
    {
        return $this->hasMany(AdminCustomerNote::class);
    }

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