<?php

declare(strict_types=1);

namespace App\Framework\MachineLearning\Migrations;

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

/**
 * Create ML Models Table
 *
 * Stores machine learning model metadata including:
 * - Model identification (name, version)
 * - Model type (supervised, unsupervised, reinforcement)
 * - Configuration and performance metrics (JSON)
 * - Deployment status and environment
 */
final class CreateMlModelsTable implements Migration
{
    public function up(ConnectionInterface $connection): void
    {
        $schema = new Schema($connection);

        $schema->createIfNotExists('ml_models', function (Blueprint $table) {
            // Primary identification
            $table->string('model_name', 255);
            $table->string('version', 50);

            // Model metadata
            $table->string('model_type', 50); // supervised, unsupervised, reinforcement
            $table->text('configuration'); // JSON: hyperparameters, architecture, etc.
            $table->text('performance_metrics'); // JSON: accuracy, precision, recall, etc.

            // Deployment tracking
            $table->boolean('is_deployed')->default(false);
            $table->string('environment', 50); // development, staging, production

            // Documentation
            $table->text('description')->nullable();

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

            // Primary key: model_name + version
            $table->primary('model_name', 'version');

            // Indexes for efficient queries
            $table->index(['model_type'], 'idx_ml_models_type');
            $table->index(['environment', 'is_deployed'], 'idx_ml_models_env');
            $table->index(['created_at'], 'idx_ml_models_created');
        });

        $schema->execute();
    }

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

    public function getVersion(): MigrationVersion
    {
        return MigrationVersion::fromTimestamp("2025_01_26_100000");
    }

    public function getDescription(): string
    {
        return "Create ML models metadata table";
    }
}
