<?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::create('trade_periods', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('duration_seconds');
            $table->decimal('profit_percent', 8, 2);
            $table->decimal('min_amount', 20, 8);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('trades', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('market_id')->constrained()->cascadeOnDelete();
            $table->foreignId('trade_period_id')->constrained()->cascadeOnDelete();
            $table->string('direction');
            $table->decimal('amount', 20, 8);
            $table->string('status')->default('active');
            $table->string('outcome')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->timestamp('closed_at')->nullable();
            $table->decimal('gross_payout', 20, 8)->nullable();
            $table->decimal('fees', 20, 8)->default(0);
            $table->decimal('net_pnl', 20, 8)->nullable();
            $table->decimal('balance_before', 20, 8)->nullable();
            $table->decimal('balance_after', 20, 8)->nullable();
            $table->decimal('tick_price', 20, 8)->nullable();
            $table->timestamps();
        });

        Schema::create('transfers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('from_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('to_user_id')->constrained('users')->cascadeOnDelete();
            $table->decimal('amount', 20, 8);
            $table->string('status')->default('completed');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('transfers');
        Schema::dropIfExists('trades');
        Schema::dropIfExists('trade_periods');
    }
};
