<?php

declare(strict_types=1);

namespace App\Framework\ErrorReporting\Migrations;

use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\Migration\Migration;
use App\Framework\Database\Migration\MigrationVersion;
use App\Framework\Database\Schema\Schema;

/**
 * Create error_reports table for structured error reporting
 */
final class Migration_2024_01_25_100000_CreateErrorReportsTable implements Migration
{
    public function up(ConnectionInterface $connection): void
    {
        $schema = new Schema($connection);

        $schema->createIfNotExists('error_reports', function ($table) {
            // Primary identification
            $table->string('id', 32)->primary();
            $table->timestamp('timestamp')->index();

            // Error details
            $table->string('level', 20)->index();
            $table->text('message');
            $table->string('exception', 255)->index();
            $table->text('file');
            $table->integer('line');
            $table->longText('trace');
            $table->json('context');

            // User context
            $table->string('user_id', 36)->nullable()->index();
            $table->string('session_id', 128)->nullable()->index();

            // Request context
            $table->string('request_id', 64)->nullable()->index();
            $table->text('user_agent')->nullable();
            $table->string('ip_address', 45)->nullable()->index();
            $table->string('route', 255)->nullable()->index();
            $table->string('method', 10)->nullable()->index();
            $table->json('request_data')->nullable();

            // Performance context
            $table->float('execution_time')->nullable()->index();
            $table->integer('memory_usage')->nullable();

            // Metadata
            $table->json('tags');
            $table->json('breadcrumbs');
            $table->string('release', 50)->nullable()->index();
            $table->string('environment', 20)->nullable()->index();
            $table->json('server_info');
            $table->json('custom_data');

            // Analytics
            $table->string('fingerprint', 16)->index();
            $table->tinyInteger('severity_level')->index();

            // Timestamps
            $table->timestamp('created_at')->default('CURRENT_TIMESTAMP');

            // Indexes for common queries
            $table->index(['timestamp', 'level']);
            $table->index(['fingerprint', 'timestamp']);
            $table->index(['environment', 'timestamp']);
            $table->index(['route', 'timestamp']);
            $table->index(['user_id', 'timestamp']);
            $table->index(['severity_level', 'timestamp']);
        });

        $schema->execute();
    }

    public function down(ConnectionInterface $connection): void
    {
        $schema = new Schema($connection);

        $schema->dropIfExists('error_reports');

        $schema->execute();
    }

    public function getVersion(): MigrationVersion
    {
        return MigrationVersion::fromTimestamp("2024_01_25_100000");
    }

    public function getDescription(): string
    {
        return 'Create error_reports table for structured error reporting and analytics';
    }
}
