<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

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

    protected $fillable = [
        'name',
        'nama_rek',
        'email',
        'password',
        'telp',
        'ref_code',
        'ref_by', // ✅ TAMBAH INI
        'extplayer',
    ];

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

    /* ================= RELATION ================= */
    
    /* ================= REFERRAL ================= */

public function referrer()
{
    return $this->belongsTo(User::class, 'ref_by');
}

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

    public function saldo()
    {
        return $this->hasOne(Saldo::class, 'user_id');
    }

    public function bankAccount()
    {
        return $this->hasOne(BankAccount::class, 'user_id');
    }

    public function transaksi()
    {
        return $this->hasMany(Transaksi::class, 'user_id');
    }


    #region dashboard

    function get_summary_member(){
        $date_yest = date("Y-m-d", strtotime(date('Y-m-d') ." -1 day") );

        $src_today = " BETWEEN '".date('Y-m-d 00:00:00')."' AND '".date('Y-m-d 23:59:59')."'";
        $src_yesterday = " BETWEEN '".date($date_yest.' 00:00:00')."' AND '".date($date_yest.' 23:59:59')."'";
        $src_month = " BETWEEN '".date('Y-m-01 00:00:00')."' AND '".date('Y-m-d 23:59:59')."'";
        $src_all = " BETWEEN '".date('2000 00:00:00')."' AND '".date('Y-m-d 23:59:59')."'";

        $sql = "SELECT 
            IFNULL((
                SELECT
                    COUNT(usr.id)
                FROM
                    users usr
                WHERE
                    usr.created_at {$src_today}
                    AND usr.level IS NULL
                    
            ),0) as trans_now,
            IFNULL((
                SELECT
                    COUNT(usr.id)
                FROM
                    users usr
                WHERE
                    usr.created_at {$src_yesterday}
                    AND usr.level IS NULL
                    
            ),0) as trans_yesterday,
            IFNULL((
                SELECT
                    COUNT(usr.id)
                FROM
                    users usr
                WHERE
                    usr.created_at {$src_month}
                    AND usr.level IS NULL
                    
            ),0) as trans_month,
            IFNULL((
                SELECT
                    COUNT(usr.id)
                FROM
                    users usr
                WHERE
                    usr.created_at {$src_all}
                    AND usr.level IS NULL
                    
            ),0) as trans_all
            ";

        $query = DB::select($sql)[0];

        return $query;
    }
    #endregion
}
