-- ============================================================================
-- Migration 020 - Asset Relevance Scoring (NIS2 GV.OC-04)
-- ----------------------------------------------------------------------------
-- Aggiunge la metodologia di scoring rilevanza NIS2 (0-100, 6 criteri pesati)
-- alla tabella assets. Adattata dai mockup docs/nis2/assets.html +
-- doc-relevant-systems.html (Determina/metodologia CdA, soglia >=40 rilevante).
--
-- Criteri: C1 Criticita Operativa (0-25), C2 Impatto Interruzione (0-25),
--          C3 Dati Trattati (0-20), C4 Dipendenze (0-15),
--          C5 Esposizione (0-10), C6 Obblighi Normativi (0-5).
-- Classificazione: >=80 critico | 60-79 alto | 40-59 medio | 20-39 basso | <20 trascurabile
-- Rilevanza NIS2: score >= 40.
--
-- IMPORTANTE (vedi CLAUDE.md / memoria): MySQL 8 Ubuntu NON supporta
-- "ADD COLUMN IF NOT EXISTS". Questo script usa una stored procedure idempotente
-- che verifica information_schema prima di ogni ALTER. Rilanciabile senza danni.
-- Eseguire con:  mysql -h localhost nis2_agile_db -e "source docs/sql/020_asset_relevance.sql"
-- ============================================================================

DELIMITER //

DROP PROCEDURE IF EXISTS _mig020_add_col //
CREATE PROCEDURE _mig020_add_col(IN col VARCHAR(64), IN ddl TEXT)
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'assets' AND COLUMN_NAME = col
    ) THEN
        SET @sql = CONCAT('ALTER TABLE assets ADD COLUMN ', ddl);
        PREPARE st FROM @sql; EXECUTE st; DEALLOCATE PREPARE st;
    END IF;
END //

DELIMITER ;

CALL _mig020_add_col('relevance_score',       "relevance_score TINYINT UNSIGNED NULL COMMENT 'Punteggio rilevanza NIS2 0-100'");
CALL _mig020_add_col('relevance_criteria',    "relevance_criteria JSON NULL COMMENT 'Dettaglio punteggi C1-C6 per audit'");
CALL _mig020_add_col('relevance_class',       "relevance_class ENUM('critico','alto','medio','basso','trascurabile') NULL");
CALL _mig020_add_col('is_nis2_relevant',      "is_nis2_relevant TINYINT(1) NOT NULL DEFAULT 0 COMMENT '1 se score >= 40'");
CALL _mig020_add_col('relevance_assessed_at', "relevance_assessed_at DATETIME NULL");
CALL _mig020_add_col('relevance_assessed_by', "relevance_assessed_by INT NULL");

DROP PROCEDURE IF EXISTS _mig020_add_col;

-- Indice per filtri "sistemi rilevanti" (idempotente via check)
DELIMITER //
DROP PROCEDURE IF EXISTS _mig020_add_idx //
CREATE PROCEDURE _mig020_add_idx()
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'assets' AND INDEX_NAME = 'idx_relevance'
    ) THEN
        ALTER TABLE assets ADD INDEX idx_relevance (is_nis2_relevant, relevance_score);
    END IF;
END //
DELIMITER ;
CALL _mig020_add_idx();
DROP PROCEDURE IF EXISTS _mig020_add_idx;

-- ROLLBACK (manuale):
-- ALTER TABLE assets
--   DROP COLUMN relevance_score, DROP COLUMN relevance_criteria,
--   DROP COLUMN relevance_class, DROP COLUMN is_nis2_relevant,
--   DROP COLUMN relevance_assessed_at, DROP COLUMN relevance_assessed_by,
--   DROP INDEX idx_relevance;
