<?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;
use Spatie\Permission\Traits\HasRoles; 
use Spatie\Permission\Models\Role;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'phone',
        'address',
        'avatar',
        'is_active',
        'role',
        'balance',
        'trust_score',
        'language',
        'is_verified',
        // 'role_id', // optional: Spatie manages roles in pivot table
    ];

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

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'balance' => 'decimal:2',
        'trust_score' => 'decimal:2',
        'is_verified' => 'boolean',
    ];

    /**
     * Optional role relationship (if you still use role_id)
     */
    public function role()
    {
        return $this->belongsTo(Role::class);
    }

    public function sharedSubscriptions()
    {
        return $this->hasMany(SharedSubscription::class, 'owner_id');
    }

    public function subscriptionMembers()
    {
        return $this->hasMany(SubscriptionMember::class);
    }

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

    public function payoutRequests()
    {
        return $this->hasMany(PayoutRequest::class);
    }

    public function trustScores()
    {
        return $this->hasMany(TrustScore::class);
    }

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

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

    public function referrals()
    {
        return $this->hasMany(Referral::class, 'referrer_id');
    }
}
