<?php

/**
 * 文件功能：改良 marriages 表，添加婚姻系统所需新字段
 * 同时为 users 表添加 frozen_jjb（定时婚礼金币冻结）字段
 */

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

return new class extends Migration
{
    /**
     * 执行迁移：改良 marriages 表，扩展 user_purchases.status，新增 users.frozen_jjb。
     */
    public function up(): void
    {
        // ── marriages 表：新增所有业务字段 ─────────────────────────
        Schema::table('marriages', function (Blueprint $table) {
            // 关联用户（将旧字符串字段升级为 FK）
            $table->unsignedBigInteger('user_id')->nullable()->after('id')->index()->comment('求婚发起方 user.id');
            $table->unsignedBigInteger('partner_id')->nullable()->after('user_id')->index()->comment('被求婚方 user.id');

            // 使用的戒指
            $table->unsignedBigInteger('ring_item_id')->nullable()->after('partner_id')->comment('使用的戒指 shop_items.id');
            $table->unsignedBigInteger('ring_purchase_id')->nullable()->after('ring_item_id')->comment('消耗的购买记录 user_purchases.id');

            // 婚姻状态
            $table->enum('status', ['pending', 'married', 'divorced', 'rejected', 'expired'])
                ->default('pending')->after('ring_purchase_id')->comment('婚姻状态');

            // 关键时间节点
            $table->timestamp('proposed_at')->nullable()->after('status')->comment('求婚时间');
            $table->timestamp('expires_at')->nullable()->after('proposed_at')->comment('求婚48h后过期时间');
            $table->timestamp('married_at')->nullable()->after('expires_at')->comment('结婚时间');
            $table->timestamp('divorced_at')->nullable()->after('married_at')->comment('离婚时间');

            // 离婚相关
            $table->enum('divorce_type', ['mutual', 'forced', 'auto', 'admin'])
                ->nullable()->after('divorced_at')->comment('离婚类型');
            $table->unsignedBigInteger('divorcer_id')->nullable()->after('divorce_type')->comment('强制离婚发起方 user.id');
            $table->timestamp('divorce_requested_at')->nullable()->after('divorcer_id')->comment('协议离婚申请时间（用于72h超时检测）');

            // 亲密度
            $table->integer('intimacy')->default(0)->after('divorce_requested_at')->comment('当前亲密度积分');
            $table->tinyInteger('level')->unsigned()->default(1)->after('intimacy')->comment('婚姻等级（1-4）');

            // 统计字段
            $table->integer('online_minutes')->default(0)->after('level')->comment('双方同时在线累计分钟数');
            $table->integer('flower_count')->default(0)->after('online_minutes')->comment('相互送花累计次数');
            $table->integer('chat_count')->default(0)->after('flower_count')->comment('私聊消息累计条数');

            // 管理员备注
            $table->string('admin_note', 200)->nullable()->after('chat_count')->comment('管理员操作备注');
        });

        // ── users 表：新增定时婚礼冻结金币字段 ──────────────────────
        Schema::table('users', function (Blueprint $table) {
            $table->integer('frozen_jjb')->default(0)->after('jjb')->comment('定时婚礼预冻结金币（待婚礼触发后正式扣除）');
        });
    }

    /**
     * 回滚：移除新增字段。
     */
    public function down(): void
    {
        Schema::table('marriages', function (Blueprint $table) {
            $table->dropColumn([
                'user_id', 'partner_id', 'ring_item_id', 'ring_purchase_id',
                'status', 'proposed_at', 'expires_at', 'married_at', 'divorced_at',
                'divorce_type', 'divorcer_id', 'divorce_requested_at',
                'intimacy', 'level', 'online_minutes', 'flower_count', 'chat_count',
                'admin_note',
            ]);
        });

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('frozen_jjb');
        });
    }
};
