<?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;
use Laravel\Sanctum\HasApiTokens;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'role',
        'email',
        'password',
        'password_show',
        'amount',
        'rcode',
        'owncode',
        'status',
        'state',
        'city',
        'address'
    ];

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

    public function rechargeSummery()
    {
        return $this->hasMany(RechargeSummery::class);
    }

    public function banks()
    {
        return $this->hasMany(BankDetail::class);
    }

    public function withdrawal()
    {
        return $this->hasMany(Withdrawal::class);
    }

    public function userTotalwithdrawal()
    {
        return $this->hasMany(Withdrawal::class)->where("status", '=', 1)->sum('payout');
    }

    public function commissions()
    {
        return $this->hasMany(Commission::class, 'commission_user_id', 'id')->orderBy('created_at', 'DESC');
    }

    public function userTotalCommissions()
    {
        return round($this->hasMany(Commission::class, 'commission_user_id', 'id')->sum('commission'), 2);
    }

    public function bonuses()
    {
        return $this->hasMany(Bonus::class, 'bonus_user_id', 'id')->where('bonus', '>=', 0)->orderBy('created_at', 'DESC');
    }

    public function totalBonusesAmount()
    {
        return round($this->hasMany(Bonus::class, 'bonus_user_id', 'id')->sum('bonus'), 2);
    }

    public function interests()
    {
        return $this->hasMany(Interest::class)->orderBy('created_at', 'DESC');
    }

    public function userTotalInterests()
    {
        return round($this->hasMany(Interest::class)->sum('interest'), 2);
    }

    public function latestInterest()
    {
        return $this->hasOne(Interest::class)->latestOfMany('created_at');
    }

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

    public function getTotalRecharge()
    {
        return round($this->rechargeSummery()->where('status', '=', 1)->sum('amount'), 2);
    }

    public function userBettingWonAmount()
    {
        return round($this->hasMany(Betting::class)->where('result', '=', 1)->sum('winning_amount'), 2);
    }

    public function userTotalBet()
    {
        return round($this->hasMany(Betting::class)->sum('amount'), 2);
    }

    //upiTansaction
    public function upiTransactions()
    {
        return $this->hasMany(UpiTransaction::class);
    }
}
