<?php
// Seeder catalogo requisiti ACN da docs/nis2/allegati_acn/acn_requirements.json (fonte: Allegati 1-2 ACN).
// Idempotente: ON DUPLICATE KEY UPDATE sul vincolo (entity, subcategory, req_index).
chdir(dirname(__DIR__));
require_once 'application/config/config.php';
require_once 'application/config/database.php';

$json = json_decode(file_get_contents('docs/nis2/allegati_acn/acn_requirements.json'), true);
if (!$json) { fwrite(STDERR, "JSON non leggibile\n"); exit(1); }

$n = 0;
foreach (['importante','essenziale'] as $entity) {
    foreach ($json[$entity] as $r) {
        Database::query(
            'INSERT INTO acn_requirements (entity, function_name, subcategory, subcategory_text, req_index, requirement)
             VALUES (?,?,?,?,?,?)
             ON DUPLICATE KEY UPDATE function_name=VALUES(function_name), subcategory_text=VALUES(subcategory_text), requirement=VALUES(requirement)',
            [$entity, $r['function'], $r['subcategory'], $r['subcategory_text'] ?? null, $r['req_index'], $r['requirement']]
        );
        $n++;
    }
}
$imp = Database::fetchOne("SELECT COUNT(*) c FROM acn_requirements WHERE entity='importante'")['c'];
$ess = Database::fetchOne("SELECT COUNT(*) c FROM acn_requirements WHERE entity='essenziale'")['c'];
echo "Seed completato: processati $n | DB importanti=$imp essenziali=$ess\n";
