<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * 创建用户表（含所有字段，合并历次 add_column 迁移）
     */
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('username', 50)->unique()->comment('用户名');
            $table->string('password')->comment('密码 (MD5 or Bcrypt)');
            $table->string('email', 250)->nullable()->comment('邮箱');
            $table->string('question', 100)->nullable()->comment('密保问题');
            $table->string('answer', 100)->nullable()->comment('密保答案');
            $table->tinyInteger('sex')->default(0)->comment('性别 (0保密 1男 2女)');
            $table->string('sign', 255)->nullable()->comment('个性签名');
            $table->tinyInteger('user_level')->default(1)->comment('用户等级');
            $table->dateTime('log_time')->nullable()->comment('登录时间');
            $table->integer('visit_num')->default(0)->comment('访问次数');
            $table->dateTime('in_time')->nullable()->comment('进房时间');
            $table->tinyInteger('out_info')->default(0)->comment('退出信息');
            $table->dateTime('out_time')->nullable()->comment('退出时间');
            $table->integer('exp_num')->default(0)->index()->comment('经验值');
            $table->tinyInteger('f_size')->nullable()->comment('字体大小');
            $table->tinyInteger('l_height')->nullable()->comment('行高');
            $table->tinyInteger('n_color')->nullable()->comment('名称颜色');
            // s_color 直接以 varchar 存储 hex 颜色（如 #ff0000）
            $table->string('s_color', 10)->nullable()->comment('发言颜色（hex，如 #ff0000）');
            $table->string('remand', 250)->nullable()->comment('密码提示问题');
            $table->string('bgcolor', 50)->nullable()->comment('背景颜色');
            $table->string('temppass', 20)->nullable()->comment('临时密码');
            $table->string('oicq', 30)->nullable()->comment('QQ号');
            $table->tinyInteger('saved')->nullable()->comment('是否保存');
            $table->string('first_ip', 50)->nullable()->comment('首次IP');
            $table->string('last_ip', 50)->nullable()->comment('最后IP');
            $table->string('aihaos', 250)->nullable()->comment('爱好');
            $table->string('friends', 250)->nullable()->comment('好友列表');
            $table->integer('headface')->nullable()->comment('头像');
            $table->integer('room_id')->default(0)->index()->comment('所在房间');
            $table->tinyInteger('auto_update')->nullable()->comment('自动刷新');
            $table->string('ppass', 50)->nullable()->comment('二级密码');
            $table->integer('jjb')->default(0)->comment('交友币/金币');
            $table->string('love', 50)->nullable()->comment('伴侣');
            $table->string('gzdw', 50)->nullable()->comment('工作单位');
            $table->integer('photo')->nullable()->comment('照片');
            $table->integer('hj')->default(0)->comment('呼叫状态');
            $table->string('djname', 50)->nullable()->comment('等级名称');
            $table->string('usersf', 50)->nullable()->comment('用户身份（头像图片名）');
            $table->integer('yh')->nullable()->comment('隐身状态');
            $table->text('userpassword')->nullable()->comment('备用密码');
            $table->string('huiyuan', 50)->nullable()->comment('会员组别');
            // VIP 会员等级 ID（关联 vip_levels 表）
            $table->unsignedBigInteger('vip_level_id')->nullable()->comment('会员等级ID');
            $table->dateTime('hy_time')->nullable()->comment('会员到期时间');
            $table->string('tuijian', 50)->nullable()->comment('推荐人');
            $table->string('tuijian_ip', 50)->nullable()->comment('推荐IP');
            $table->string('xiaohai', 50)->nullable()->comment('小孩');
            $table->string('qingren', 50)->nullable()->comment('情人');
            $table->string('zhufang', 50)->nullable()->comment('住房');
            $table->string('zuoqiimg', 50)->nullable()->comment('坐骑图片');
            $table->string('zuoqi', 50)->nullable()->comment('坐骑名称');
            $table->integer('guanli')->nullable()->comment('管理员标记');
            $table->integer('meili')->nullable()->comment('魅力值');
            $table->integer('teshu')->nullable()->comment('特殊权限');
            $table->dateTime('xr_time')->nullable()->comment('仙人时间');
            $table->dateTime('yx_time')->nullable()->comment('英雄时间');
            $table->integer('q1')->nullable();
            $table->integer('q2')->nullable();
            $table->integer('q3')->nullable();
            $table->string('hua', 255)->nullable()->comment('鲜花');
            $table->dateTime('sj')->nullable()->comment('注册/更新时间');
            $table->string('pig', 255)->nullable()->comment('宠物');
            $table->text('qianming')->nullable()->comment('个性签名（旧字段）');
            $table->dateTime('q3_time')->nullable();
            // 新人礼包是否已领取（首次入场赠送 6666 金币）
            $table->boolean('has_received_new_gift')->default(false)->comment('是否已领取新人礼包');

            $table->rememberToken();
            $table->timestamps();

            // VIP 外键（引用 vip_levels 表，vip_levels 在后续迁移中创建）
            // 注意：外键在 create_vip_levels_table 之后才添加
        });

        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->longText('payload');
            $table->integer('last_activity')->index();
        });
    }

    /**
     * 回滚：删除用户相关表
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('sessions');
    }
};
