<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    protected $fillable = [
        'full_name',
        'email',
        'phone',
        'password_hash',
        'avatar',
        'status',
        'email_verified',
        'phone_verified',
        'two_factor_enabled',
        'two_factor_secret',
        'wallet_balance',
        'points',
        'level',
        'experience',
        'referral_code',
        'referred_by',
        'last_login',
        'login_count',
    ];

    protected $hidden = [
        'password_hash',
        'remember_token',
        'two_factor_secret',
    ];

    protected $casts = [
        'email_verified' => 'boolean',
        'phone_verified' => 'boolean',
        'two_factor_enabled' => 'boolean',
        'last_login' => 'datetime',
        'wallet_balance' => 'decimal:2',
    ];

    public function getAuthPasswordName()
    {
        return 'password_hash';
    }

    public function setPasswordAttribute($value)
    {
        $this->attributes['password_hash'] = bcrypt($value);
    }

    // Relationships
    public function profile()
    {
        return $this->hasOne(UserProfile::class);
    }

    public function cycles()
    {
        return $this->hasMany(Cycle::class);
    }

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

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