<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('tradings', function (Blueprint $table) {
            // General order fields
            $table->string('trading_type')->default('Spot'); // Spot or Futures
            $table->decimal('limit_price', 20, 8)->nullable();
            $table->decimal('trigger_price', 20, 8)->nullable();
            $table->decimal('order_quantity', 20, 8)->nullable();
            $table->decimal('filled_quantity', 20, 8)->default(0);
            $table->decimal('avg_fill_price', 20, 8)->nullable();
            $table->decimal('fees', 20, 8)->default(0);
            $table->boolean('post_only')->default(false);
            $table->boolean('reduce_only')->default(false);
            $table->string('time_in_force')->default('GTC'); // GTC, IOC, FOK
            
            // Futures specific
            $table->decimal('leverage', 5, 2)->nullable();
            $table->string('margin_type')->nullable(); // Cross, Isolated
            
            // TWAP specific
            $table->integer('twap_duration')->nullable(); // minutes
            $table->string('twap_frequency')->nullable(); // 30S, 1M, 3M, 5M, 10M, 15M, 30M
            $table->decimal('twap_quantity_per_order', 20, 8)->nullable();
            $table->boolean('twap_randomize')->default(false);
            
            // Iceberg specific
            $table->decimal('iceberg_visible_quantity', 20, 8)->nullable();
            $table->string('iceberg_chase_option')->nullable(); // Chase Limit options
            
            // Scaled Order specific
            $table->decimal('scaled_min_price', 20, 8)->nullable();
            $table->decimal('scaled_max_price', 20, 8)->nullable();
            $table->integer('scaled_orders_count')->nullable();
            $table->text('scaled_orders_data')->nullable(); // JSON data for individual orders
            
            // Trailing Stop specific
            $table->string('trailing_type')->nullable(); // Percentage, Amount
            $table->decimal('trailing_value', 20, 8)->nullable();
            $table->decimal('activation_price', 20, 8)->nullable();
            
            // Chase Limit Order specific
            $table->string('chase_price_type')->nullable(); // Bid1, Ask1, Bid2, Ask2, Bid3, Ask3, Last Price
            $table->decimal('max_chase_distance', 20, 8)->nullable();
            $table->string('chase_interval')->nullable(); // 1S, 2S, 5S, 10S, 30S
            
            // OCO specific
            $table->decimal('oco_stop_price', 20, 8)->nullable();
            $table->decimal('oco_limit_price', 20, 8)->nullable();
            
            // Conditional specific
            $table->string('condition_type')->nullable(); // LastPrice, MarkPrice, IndexPrice
            $table->string('condition_operator')->nullable(); // >=, <=
            $table->decimal('condition_value', 20, 8)->nullable();
            
            // Order management
            $table->timestamp('triggered_at')->nullable();
            $table->timestamp('filled_at')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->text('admin_notes')->nullable();
            $table->boolean('admin_modified')->default(false);
            $table->timestamp('admin_modified_at')->nullable();
            $table->json('order_history')->nullable(); // Track order modifications
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('tradings', function (Blueprint $table) {
            $table->dropColumn([
                'trading_type', 'limit_price', 'trigger_price', 'order_quantity', 'filled_quantity',
                'avg_fill_price', 'fees', 'post_only', 'reduce_only', 'time_in_force',
                'leverage', 'margin_type', 'twap_duration', 'twap_frequency', 'twap_quantity_per_order',
                'twap_randomize', 'iceberg_visible_quantity', 'iceberg_chase_option',
                'scaled_min_price', 'scaled_max_price', 'scaled_orders_count', 'scaled_orders_data',
                'trailing_type', 'trailing_value', 'activation_price', 'chase_price_type',
                'max_chase_distance', 'chase_interval', 'oco_stop_price', 'oco_limit_price',
                'condition_type', 'condition_operator', 'condition_value', 'triggered_at',
                'filled_at', 'rejection_reason', 'admin_notes', 'admin_modified',
                'admin_modified_at', 'order_history'
            ]);
        });
    }
};