-- Migration 109: Vollstaendige Template-Bereinigung
-- Entfernt Duplikate, behaelt jeweils die neueste/groesste Version

-- ===========================================================================
-- 1. DPA: Behalte v2 DE (unseres aus 088), loesche v1 DE + v1 EN
-- ===========================================================================
DELETE FROM compliance_legal_templates
WHERE document_type = 'dpa'
  AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
  AND version != '2.0'
  AND language = 'de';

DELETE FROM compliance_legal_templates
WHERE document_type = 'dpa'
  AND language = 'en'
  AND version = '1.0.0'
  AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e';

-- ===========================================================================
-- 2. Cookie-Banner: Behalte das groessere (IF-Bloecke), loesche das kleinere
-- ===========================================================================
DELETE FROM compliance_legal_templates
WHERE document_type = 'cookie_banner'
  AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
  AND id != (
    SELECT id FROM compliance_legal_templates
    WHERE document_type = 'cookie_banner'
      AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
    ORDER BY length(content) DESC
    LIMIT 1
  );

-- ===========================================================================
-- 3. Impressum: Behalte das groessere (IF-Bloecke), loesche das kleinere
-- ===========================================================================
DELETE FROM compliance_legal_templates
WHERE document_type = 'impressum'
  AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
  AND id != (
    SELECT id FROM compliance_legal_templates
    WHERE document_type = 'impressum'
      AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
    ORDER BY length(content) DESC
    LIMIT 1
  );

-- ===========================================================================
-- 4. TOM: Behalte das neueste, loesche Duplikat
-- ===========================================================================
DELETE FROM compliance_legal_templates
WHERE document_type = 'tom_documentation'
  AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
  AND id != (
    SELECT id FROM compliance_legal_templates
    WHERE document_type = 'tom_documentation'
      AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
    ORDER BY updated_at DESC
    LIMIT 1
  );

-- ===========================================================================
-- 5. DSFA: Behalte v2 (groesser), loesche v1
-- ===========================================================================
DELETE FROM compliance_legal_templates
WHERE document_type = 'dsfa'
  AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
  AND id != (
    SELECT id FROM compliance_legal_templates
    WHERE document_type = 'dsfa'
      AND tenant_id = '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
    ORDER BY length(content) DESC
    LIMIT 1
  );

-- ===========================================================================
-- 6. VVT: NICHT loeschen — alle 7 behalten (Branchenvorlagen sind wertvoll)
-- ===========================================================================
-- Keine Aktion
