-- Migration 029: IACE Mitigation Relevance + Customer-Standard flag
-- ==========================================================================
-- The engine generates ~80 mitigations per project (Bremsscheibe benchmark).
-- Many are not applicable for a specific customer site — e.g. the customer
-- has 30 of them already implemented as company-wide standard. To keep the
-- verification step meaningful, the expert needs to:
--
--   1. Mark each mitigation as relevant (or delete it from the project),
--   2. Optionally flag it as "customer standard — no evidence required".
--
-- This is the difference between "applicable, must be verified" and
-- "applicable, but the expert already knows it's covered by the customer's
-- existing setup". Both must reach the verification report; only the first
-- needs an evidence upload.
--
-- A later feature reuses is_customer_standard to suggest pre-marked
-- mitigations when the same customer commissions another plant assessment.
-- ==========================================================================

-- is_relevant: Fachmann hat die Massnahme als anwendbar bestaetigt.
--   FALSE  → Engine-Vorschlag, vom Fachmann noch nicht bewertet.
--   TRUE   → Fachmann hat 'Relevant' angekreuzt; geht in die Verifikation.
-- is_customer_standard: Beim Kunden bereits implementiert.
--   FALSE  → benötigt Nachweis in der Verifikation.
--   TRUE   → keine Evidence-Datei notwendig; gilt als verifiziert.
ALTER TABLE iace_mitigations
    ADD COLUMN IF NOT EXISTS is_relevant          BOOLEAN NOT NULL DEFAULT FALSE,
    ADD COLUMN IF NOT EXISTS is_customer_standard BOOLEAN NOT NULL DEFAULT FALSE;

-- An is_customer_standard mitigation is by definition relevant.
ALTER TABLE iace_mitigations
    DROP CONSTRAINT IF EXISTS iace_mitigations_customer_standard_chk,
    ADD CONSTRAINT iace_mitigations_customer_standard_chk
        CHECK (is_customer_standard = FALSE OR is_relevant = TRUE);

-- Index for the verification-page filter (`WHERE is_relevant = TRUE`).
CREATE INDEX IF NOT EXISTS idx_iace_mitigations_relevant
    ON iace_mitigations(is_relevant)
    WHERE is_relevant = TRUE;
