<?php

namespace App\Models;

// // 关键：导入正确的 Builder 类（Eloquent 构建器）
// use Illuminate\Database\Eloquent\Builder;

use Carbon\Carbon;

/**
 * @property int $id
 * @property string $member_id
 * @property string $username
 * @property string $first_name
 * @property string $language
 * @property string $register_ip
 * @property integer $status
 * @property string $visitor_id
 * @property string $phone 用户手机号
 * @property string $admin_note
 * @property $last_active_time 最后活跃时间
 */
class User extends BaseModel
{
    protected $table = 'users';
    protected $fillable = ['usdt', 'is_banned', 'last_active_time', 'visitor_id', 'register_ip', 'register_domain', 'status', 'admin_note', 'member_id', 'first_name', 
    'game_id', 'username', 'secret_key', 'secret_pass', 'language','user_code', 'agent_user_code','level','recharge_channel_group_id'];
    protected $attributes = [
        'language' => 'zh',
    ];
    protected $hidden = ['updated_at'];

    
    // public function newQuery($excludeDeleted = true): Builder
    // {
    //     // 1. 获取原生 Eloquent 查询构建器
    //     $query = parent::newQuery($excludeDeleted);
        
    //     // 2. 强制清空当前连接的表前缀（从根源阻止拼接）
    //     $this->getConnection()->setTablePrefix('');
        
    //     // 3. 强制指定查询的表名为 la_operation（覆盖所有拼接逻辑）
    //     $query->from('bot_users');
        
    //     return $query;
    // }
    
    function getLastActiveTimeAttribute($value): string
    {
        if ($value > 0 && !is_numeric($value)) {
            return date('Y-m-d H:i', strtotime($value));
        }
        return "";
    }

    function getCreatedAtAttribute($value): string
    {
        return Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d');
    }

    public function wallet()
    {
        return $this->belongsTo(Wallet::class, 'member_id', 'member_id');
    }

    public function level()
    {
        return $this->belongsTo(Level::class, 'level', 'level')->select('id', 'level', 'level_name','img','recharge');
    }

    public function agent()
    {
        return $this->belongsTo(User::class, 'agent_user_code', 'user_code')->select('id', 'user_code', 'account','first_name','member_id');
    }

    public function setLanguage($language): void
    {
        $this->language = $language;
    }

    public function getMemberId()
    {
        return $this->member_id;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getFirstName()
    {
        return $this->first_name;
    }

    /**
     * 生成8位数字+大写字母混合邀请码
     * @return string 生成的邀请码
     */
    public static function generateUserCode($num_len = 6, $letter_len = 2) {
        // 生成num_len位安全随机数字
        $numbers = '';
        for ($i = 0; $i < $num_len; $i++) {
            $numbers .= random_int(0, 9);
        }
        
        // 生成letter_len位安全随机字母
        $letters = '';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charLength = strlen($chars);
        for ($i = 0; $i < $letter_len; $i++) {
            $letters .= $chars[random_int(0, $charLength - 1)];
        }
        
        // 打乱顺序
        $user_code = str_shuffle($numbers . $letters);
        if (User::where('user_code', $user_code)->value('id')) {
            return self::generateUserCode();
        }
        return $user_code;
    }
}
