<?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('otc_candles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('otc_asset_id')->constrained()->onDelete('cascade');
            $table->enum('timeframe', ['1m', '5m', '15m', '30m', '1h', '4h', '1d'])->default('1m');
            $table->decimal('open', 20, 8);
            $table->decimal('high', 20, 8);
            $table->decimal('low', 20, 8);
            $table->decimal('close', 20, 8);
            $table->decimal('volume', 20, 8)->default(0); // Synthetic volume
            $table->integer('tick_count')->default(0); // Number of ticks in this candle
            $table->timestamp('candle_time'); // Start time of the candle
            $table->timestamp('close_time')->nullable(); // End time of the candle
            $table->boolean('is_closed')->default(false);
            $table->timestamps();

            $table->unique(['otc_asset_id', 'timeframe', 'candle_time']);
            $table->index(['otc_asset_id', 'timeframe', 'candle_time']);
            $table->index(['candle_time', 'is_closed']);
        });
    }

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

// Made with Bob
