-- Migration 022: Template Block Markers
-- ============================================================
-- This migration documents the block marker system introduced
-- in Template-Spec v1 Phase B.
--
-- Block markers are inserted into template content via the
-- Python script: scripts/apply_block_markers_022.py
--
-- Block IDs:
--   NDA_PENALTY_BLOCK         — Vertragsstrafe section (NDA DE + EN)
--   COOKIE_ANALYTICS_BLOCK    — Analyse-Tools section (Cookie Banner DE)
--   COOKIE_MARKETING_BLOCK    — Marketing-Partner section (Cookie Banner DE)
--
-- Marker syntax in template content:
--   [BLOCK:NDA_PENALTY_BLOCK]
--   ## §N Vertragsstrafe
--   ...content...
--   [/BLOCK:NDA_PENALTY_BLOCK]
--
-- The Rule Engine (ruleEngine.ts) evaluates ruleset.v1.json and
-- produces a list of block IDs to remove before placeholder substitution.
-- The applyBlockRemoval() function strips matching [BLOCK:ID]...[/BLOCK:ID]
-- sections from template content.
--
-- Run the Python migration script to apply markers to DB content:
--   docker exec bp-compliance-backend python3 /tmp/apply_block_markers_022.py
--
-- No schema changes are required — block markers are stored inline
-- in the existing `content` TEXT column of compliance_legal_templates.
-- ============================================================

-- Verify markers were applied (run after script):
SELECT
    document_type,
    language,
    title,
    CASE WHEN content LIKE '%[BLOCK:NDA_PENALTY_BLOCK]%' THEN 'YES' ELSE 'no' END AS has_nda_penalty,
    CASE WHEN content LIKE '%[BLOCK:COOKIE_ANALYTICS_BLOCK]%' THEN 'YES' ELSE 'no' END AS has_cookie_analytics,
    CASE WHEN content LIKE '%[BLOCK:COOKIE_MARKETING_BLOCK]%' THEN 'YES' ELSE 'no' END AS has_cookie_marketing
FROM compliance.compliance_legal_templates
WHERE document_type IN ('nda', 'cookie_banner')
ORDER BY document_type, language;
