<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use App\Framework\MachineLearning\ModelManagement\ModelRegistry;
use App\Framework\MachineLearning\ModelManagement\ValueObjects\ModelMetadata;
use App\Framework\MachineLearning\ModelManagement\ValueObjects\ModelType;
use App\Framework\MachineLearning\ModelManagement\ModelPerformanceMonitor;
use App\Framework\Core\ValueObjects\Version;
use App\Framework\Core\ValueObjects\Timestamp;
use App\Framework\Core\ValueObjects\Duration;
use App\Framework\Core\AppBootstrapper;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\Performance\MemoryMonitor;

echo "🌱 ML Models Seeder\n";
echo "==================\n\n";

// Bootstrap application
$basePath = dirname(__DIR__);
$clock = new SystemClock();
$highResClock = new SystemHighResolutionClock();
$memoryMonitor = new MemoryMonitor();
$collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: true);
$bootstrapper = new AppBootstrapper($basePath, $collector, $memoryMonitor);
$container = $bootstrapper->bootstrapWorker();

/** @var ModelRegistry $registry */
$registry = $container->get(ModelRegistry::class);

/** @var ModelPerformanceMonitor $performanceMonitor */
$performanceMonitor = $container->get(ModelPerformanceMonitor::class);

// Sample Models to Seed
$models = [
    // 1. Fraud Detection Model (Supervised, Production)
    [
        'name' => 'fraud-detector',
        'type' => ModelType::SUPERVISED,
        'version' => '1.0.0',
        'environment' => 'production',
        'configuration' => [
            'threshold' => 0.75,
            'min_confidence' => 0.6,
            'feature_count' => 15,
            'algorithm' => 'random_forest',
        ],
        'metrics' => [
            'accuracy' => 0.94,
            'precision' => 0.91,
            'recall' => 0.89,
            'f1_score' => 0.90,
            'total_predictions' => 15234,
            'average_confidence' => 0.87,
            'confusion_matrix' => [
                'true_positive' => 1345,
                'true_negative' => 12789,
                'false_positive' => 567,
                'false_negative' => 533,
            ],
        ],
    ],

    // 2. Spam Classifier (Supervised, Production - Degraded)
    [
        'name' => 'spam-classifier',
        'type' => ModelType::SUPERVISED,
        'version' => '2.0.0',
        'environment' => 'production',
        'configuration' => [
            'threshold' => 0.80,
            'min_confidence' => 0.7,
            'feature_count' => 20,
            'algorithm' => 'gradient_boosting',
        ],
        'metrics' => [
            'accuracy' => 0.78, // Degraded performance
            'precision' => 0.82,
            'recall' => 0.71,
            'f1_score' => 0.76,
            'total_predictions' => 8923,
            'average_confidence' => 0.75,
            'confusion_matrix' => [
                'true_positive' => 892,
                'true_negative' => 6051,
                'false_positive' => 1234,
                'false_negative' => 746,
            ],
        ],
    ],

    // 3. User Segmentation (Unsupervised, Production)
    [
        'name' => 'user-segmentation',
        'type' => ModelType::UNSUPERVISED,
        'version' => '1.2.0',
        'environment' => 'production',
        'configuration' => [
            'n_clusters' => 5,
            'algorithm' => 'k_means',
            'feature_count' => 12,
        ],
        'metrics' => [
            'accuracy' => 0.88,
            'total_predictions' => 5678,
            'average_confidence' => 0.83,
            'silhouette_score' => 0.72,
        ],
    ],

    // 4. Anomaly Detection (Unsupervised, Production)
    [
        'name' => 'anomaly-detector',
        'type' => ModelType::UNSUPERVISED,
        'version' => '1.5.0',
        'environment' => 'production',
        'configuration' => [
            'contamination' => 0.1,
            'algorithm' => 'isolation_forest',
            'feature_count' => 10,
        ],
        'metrics' => [
            'accuracy' => 0.92,
            'total_predictions' => 12456,
            'average_confidence' => 0.85,
            'anomaly_rate' => 0.08,
        ],
    ],

    // 5. Recommendation Engine (Reinforcement, Development)
    [
        'name' => 'recommendation-engine',
        'type' => ModelType::REINFORCEMENT,
        'version' => '0.5.0',
        'environment' => 'development',
        'configuration' => [
            'learning_rate' => 0.001,
            'discount_factor' => 0.95,
            'exploration_rate' => 0.1,
            'algorithm' => 'q_learning',
        ],
        'metrics' => [
            'accuracy' => 0.67, // Still in development
            'total_predictions' => 2345,
            'average_confidence' => 0.62,
            'average_reward' => 3.42,
        ],
    ],

    // 6. Sentiment Analysis (Supervised, Staging)
    [
        'name' => 'sentiment-analyzer',
        'type' => ModelType::SUPERVISED,
        'version' => '2.1.0',
        'environment' => 'staging',
        'configuration' => [
            'threshold' => 0.65,
            'algorithm' => 'lstm',
            'feature_count' => 50,
            'max_sequence_length' => 100,
        ],
        'metrics' => [
            'accuracy' => 0.91,
            'precision' => 0.89,
            'recall' => 0.92,
            'f1_score' => 0.90,
            'total_predictions' => 7890,
            'average_confidence' => 0.86,
        ],
    ],
];

echo "Registering " . count($models) . " ML models...\n\n";

foreach ($models as $index => $modelData) {
    $modelNum = $index + 1;
    echo "[$modelNum/" . count($models) . "] Registering {$modelData['name']} v{$modelData['version']}...\n";

    try {
        // Create ModelMetadata
        $metadata = new ModelMetadata(
            modelName: $modelData['name'],
            modelType: $modelData['type'],
            version: Version::fromString($modelData['version']),
            configuration: $modelData['configuration'],
            performanceMetrics: [],
            createdAt: Timestamp::now(),
            deployedAt: $modelData['environment'] === 'production' ? Timestamp::now() : null,
            environment: $modelData['environment'],
            metadata: [
                'seeded_at' => date('Y-m-d H:i:s'),
                'description' => "Sample {$modelData['type']->value} model for testing",
            ]
        );

        // Register model
        $registry->register($metadata);

        // Track performance metrics using trackPrediction
        $performanceMonitor->trackPrediction(
            modelName: $modelData['name'],
            version: Version::fromString($modelData['version']),
            prediction: 1, // Dummy prediction
            actual: 1, // Dummy actual
            confidence: $modelData['metrics']['average_confidence']
        );

        // Update metrics manually to match our sample data
        if (isset($modelData['metrics']['confusion_matrix'])) {
            $cm = $modelData['metrics']['confusion_matrix'];
            // Track individual predictions to build up confusion matrix
            for ($i = 0; $i < $cm['true_positive']; $i++) {
                $performanceMonitor->trackPrediction(
                    modelName: $modelData['name'],
                    version: Version::fromString($modelData['version']),
                    prediction: 1,
                    actual: 1,
                    confidence: $modelData['metrics']['average_confidence']
                );
            }
        }

        echo "  ✅ Successfully registered {$modelData['name']}\n";
        echo "     - Type: {$modelData['type']->value}\n";
        echo "     - Environment: {$modelData['environment']}\n";
        echo "     - Accuracy: " . round($modelData['metrics']['accuracy'] * 100, 2) . "%\n";

        if ($modelData['metrics']['accuracy'] < 0.85) {
            echo "     ⚠️  Warning: Degraded performance\n";
        }

        echo "\n";
    } catch (\Exception $e) {
        echo "  ❌ Error: {$e->getMessage()}\n\n";
    }
}

echo "==================\n";
echo "✅ Seeding complete!\n\n";

echo "Next steps:\n";
echo "1. Visit https://localhost/admin/ml/dashboard to see the models\n";
echo "2. Check API endpoint: https://localhost/api/ml/dashboard\n";
echo "3. Verify foreach attribute rendering in Models Overview table\n";
