<?php

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

/**
 * Add NEXUS engine columns to nexus_user_metrics.
 *
 * These columns are required by the Phase 3 engine classes:
 *   - consecutive_losses: Step 3 (Loss Requirement) depends on this
 *   - monthly_capital: Step 4 (Monthly Profit Cap) needs this for cap calculation
 *   - total_bids/total_wins/total_losses: metrics tracking
 *   - first_bid_win_used: first-time bidder guarantee tracking
 *
 * All columns are nullable or have defaults. Additive only. Safe on live server.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('nexus_user_metrics', function (Blueprint $table) {
            $table->unsignedInteger('consecutive_losses')->default(0)->after('total_sells_rejected');
            $table->decimal('monthly_capital', 20, 2)->default(0)->after('monthly_profit_cap');
            $table->unsignedInteger('total_bids')->default(0)->after('total_sells_rejected');
            $table->unsignedInteger('total_wins')->default(0)->after('total_bids');
            $table->unsignedInteger('total_losses')->default(0)->after('total_wins');
            $table->unsignedTinyInteger('first_bid_win_used')->default(0)->after('first_sell_granted');
        });
    }

    public function down(): void
    {
        Schema::table('nexus_user_metrics', function (Blueprint $table) {
            $table->dropColumn([
                'consecutive_losses',
                'monthly_capital',
                'total_bids',
                'total_wins',
                'total_losses',
                'first_bid_win_used',
            ]);
        });
    }
};
