<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $fillable = [
        'username', 'fullname', 'email', 'password',
        'role', 'balance', 'is_blocked', 'device_id', 'referred_by', 'referral_limit',
        'device_reset_count', 'device_reset_date',
    ];

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

    protected $casts = [
        'is_blocked'         => 'boolean',
        'balance'            => 'decimal:2',
        'device_reset_count' => 'integer',
    ];

    // JWT interface
    public function getJWTIdentifier() { return $this->getKey(); }
    public function getJWTCustomClaims() { return ['role' => $this->role, 'username' => $this->username]; }

    // Helpers
    public function isOwner()   { return $this->role === 'owner'; }
    public function isAdmin()   { return $this->role === 'admin' || $this->isOwner(); }
    public function isReseller(){ return $this->role === 'reseller'; }

    // Relations
    public function licenses()  { return $this->hasMany(License::class, 'created_by'); }
    public function referrals() { return $this->hasMany(Referral::class, 'owner_id'); }
    public function uploads()   { return $this->hasMany(Upload::class, 'uploaded_by'); }
    public function uplink()    { return $this->belongsTo(User::class, 'referred_by'); }
}
