<?php

declare(strict_types=1);

namespace App\Framework\ErrorAggregation\Migrations;

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

/**
 * Creates error_patterns table for error pattern aggregation
 */
final class Migration_2024_01_24_141000_CreateErrorPatternsTable implements Migration
{
    public function up(ConnectionInterface $connection): void
    {
        $schema = new Schema($connection);

        $schema->createIfNotExists('error_patterns', function ($table) {
            // Primary key
            $table->string('id', 26)->primary()->comment('ULID identifier');

            // Pattern identification
            $table->string('fingerprint', 64)->unique()->comment('SHA256 hash for grouping similar errors');

            // Classification (same as events for filtering)
            $table->string('service', 100)->index()->comment('Service name');
            $table->string('component', 100)->index()->comment('Component name');
            $table->string('operation', 100)->index()->comment('Operation name');
            $table->string('error_code', 10)->index()->comment('Framework error code');

            // Pattern details
            $table->text('normalized_message')->comment('Normalized error message');
            $table->string('severity', 20)->index()->comment('Error severity level');

            // Occurrence tracking
            $table->integer('occurrence_count')->default(1)->index()->comment('Number of times this pattern occurred');
            $table->timestamp('first_occurrence')->index()->comment('First time this pattern was seen');
            $table->timestamp('last_occurrence')->index()->comment('Last time this pattern was seen');

            // Impact tracking
            $table->json('affected_users')->nullable()->comment('List of affected user IDs');
            $table->json('affected_ips')->nullable()->comment('List of affected IP addresses');

            // Management
            $table->boolean('is_active')->default(true)->index()->comment('Is pattern still active');
            $table->boolean('is_acknowledged')->default(false)->index()->comment('Has been acknowledged by team');
            $table->string('acknowledged_by', 100)->nullable()->comment('Who acknowledged the pattern');
            $table->timestamp('acknowledged_at')->nullable()->comment('When pattern was acknowledged');
            $table->text('resolution')->nullable()->comment('Resolution description');

            // Additional metadata
            $table->json('metadata')->nullable()->comment('Additional pattern metadata');

            // Indexes for performance
            $table->index(['service', 'is_active']);
            $table->index(['severity', 'is_active']);
            $table->index(['occurrence_count', 'is_active']);
            $table->index(['last_occurrence', 'is_active']);
            $table->index(['is_acknowledged', 'is_active']);
        });

        $schema->execute();
    }

    public function down(ConnectionInterface $connection): void
    {
        $schema = new Schema($connection);
        $schema->dropIfExists('error_patterns');
        $schema->execute();
    }

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

    public function getDescription(): string
    {
        return 'Create error_patterns table for error pattern aggregation';
    }
}
