<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('investment_settings', function (Blueprint $table): void {
            $table->decimal('min_withdrawal_amount', 12, 2)->default(50)->after('withdrawal_fee_fixed');
        });

        Schema::create('email_confirmation_tokens', function (Blueprint $table): void {
            $table->id();
            $table->string('type', 40);
            $table->foreignId('user_id')->nullable()->constrained()->cascadeOnDelete();
            $table->foreignId('withdrawal_request_id')->nullable()->constrained()->cascadeOnDelete();
            $table->json('payload')->nullable();
            $table->string('token_hash', 64)->unique();
            $table->timestamp('expires_at');
            $table->timestamp('used_at')->nullable();
            $table->timestamps();

            $table->index(['type', 'expires_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('email_confirmation_tokens');

        Schema::table('investment_settings', function (Blueprint $table): void {
            $table->dropColumn('min_withdrawal_amount');
        });
    }
};
