<?php

declare(strict_types=1);

namespace App\Framework\Database\Migration\Migrations;

use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\Migration\Migration;
use App\Framework\Database\Migration\MigrationVersion;
use App\Framework\Database\Schema\Contracts\SchemaBuilderFactoryInterface;
use App\Framework\Database\Schema\Contracts\SchemaBuilderInterface;

final readonly class CreatePerformanceMetricsTable implements Migration
{
    public function up(ConnectionInterface $connection): void
    {
        $schemaBuilder = SchemaBuilderFactoryInterface::create($connection);

        $schemaBuilder->createTable('performance_metrics', function (SchemaBuilderInterface $table) {
            $table->id();
            $table->string('operation_id', 255)->notNull();
            $table->string('operation_type', 100)->notNull();
            $table->string('category', 50)->notNull();
            $table->string('migration_version', 100)->nullable();
            $table->integer('execution_time_ms')->notNull();
            $table->bigInteger('memory_start_bytes')->notNull();
            $table->bigInteger('memory_end_bytes')->notNull();
            $table->bigInteger('memory_peak_bytes')->notNull();
            $table->bigInteger('memory_delta_bytes')->notNull();
            $table->boolean('success')->notNull()->default(true);
            $table->text('error_message')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();

            // Indexes
            $table->index('operation_type');
            $table->index('category');
            $table->index('migration_version');
            $table->index('created_at');
            $table->index('success');
        });
    }

    public function down(ConnectionInterface $connection): void
    {
        $schemaBuilder = SchemaBuilderFactoryInterface::create($connection);
        $schemaBuilder->dropTable('performance_metrics');
    }

    public function getDescription(): string
    {
        return 'Create performance metrics table for migration and system performance tracking';
    }

    public function getVersion(): MigrationVersion
    {
        return MigrationVersion::fromTimestamp('2024_09_28_233500');
    }
}
