<?php

namespace App\Models;

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


class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'name',
        'country_code',
        'avatar',
        'balance',
        'loan_limit',
        'phone',
        'role',
        'email',
        'password',
        'status',
        'nominee_name',
        'nominee_phone',
        'nominee_relation',
    ];

    /**
     * The attributes that should be hidden for serialization.
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'balance'           => 'integer',
        'loan_limit'        => 'integer', 
    ];

    // Relation: এক user-এর অনেক loan হতে পারে
    public function loans()
    {
        return $this->hasMany(Loan::class);
    }

    

    /** সব কিস্তি (সব loan মিলিয়ে) */
    public function loanInstallments(): HasManyThrough
    {
        return $this->hasManyThrough(LoanInstallment::class, Loan::class);
    }

    /** শুধু paid কিস্তি */
    public function paidLoanInstallments(): HasManyThrough
    {
        return $this->hasManyThrough(LoanInstallment::class, Loan::class)
            ->where('loan_installments.status', 'paid');
    }
}
