-- Migration 138: Backfill aller Templates mit lifecycle_stage + functional_category
-- Setzt Default-Werte fuer alle bekannten document_types
-- Templates ohne explizite Zuordnung erhalten Default: startup+kmu+konzern, internal_policy

-- ============================================================
-- FOUNDING LEGAL (Notartermin / Vorgruendung)
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{pre_founding,founding}', functional_category = 'founding_legal'
WHERE document_type = 'gesellschafterliste';

UPDATE compliance_legal_templates SET
  lifecycle_stage = '{founding}', functional_category = 'founding_legal'
WHERE document_type IN ('gf_bestellungsbeschluss', 'hrb_anmeldung');

UPDATE compliance_legal_templates SET
  lifecycle_stage = '{pre_founding,founding,startup}', functional_category = 'founding_legal'
WHERE document_type = 'ip_assignment_agreement';

UPDATE compliance_legal_templates SET
  lifecycle_stage = '{founding,startup,kmu,konzern}', functional_category = 'founding_legal'
WHERE document_type IN ('articles_of_association', 'sha', 'geschaeftsordnung_gf');

-- ============================================================
-- INVESTOR / FUNDING
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{pre_founding,startup}', functional_category = 'investor_funding'
WHERE document_type IN ('term_sheet', 'convertible_loan_agreement');

UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu}', functional_category = 'investor_funding'
WHERE document_type IN ('subscription_agreement', 'esop_plan');

UPDATE compliance_legal_templates SET
  lifecycle_stage = '{founding,startup,kmu,konzern}', functional_category = 'investor_funding'
WHERE document_type = 'cap_table';

-- ============================================================
-- EMPLOYMENT (Mitarbeiter / Geschaeftsfuehrer)
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{founding,startup,kmu,konzern}', functional_category = 'employment'
WHERE document_type IN ('managing_director_employment_contract','employment_contract_de','nda','offboarding_policy');

-- ============================================================
-- CUSTOMER B2B
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu,konzern}', functional_category = 'customer_b2b'
WHERE document_type IN ('agb','sla','dpa','data_processing_agreement','cloud_service_agreement','terms_of_service');

-- ============================================================
-- DATA PROTECTION (DSGVO)
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu,konzern}', functional_category = 'data_protection'
WHERE document_type IN (
  'privacy_policy', 'cookie_policy', 'cookie_banner', 'consent_texts',
  'data_protection_concept', 'data_protection_policy', 'data_retention_policy',
  'data_transfer_policy', 'data_classification_policy', 'loeschkonzept',
  'pflichtenregister', 'dsfa', 'vvt_register', 'privacy_incident_policy',
  'scc_companion', 'transfer_impact_assessment', 'video_conference_dsi',
  'tom_documentation'
);

-- ============================================================
-- IT SECURITY
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu,konzern}', functional_category = 'it_security'
WHERE document_type IN (
  'it_security_concept', 'information_security_policy',
  'access_control_policy', 'access_control_concept',
  'password_policy', 'encryption_policy', 'secrets_management_policy',
  'backup_policy', 'backup_recovery_concept',
  'disaster_recovery_policy', 'business_continuity_policy', 'crisis_management_policy',
  'patch_management_policy', 'vulnerability_management_policy',
  'logging_policy', 'logging_concept',
  'byod_policy', 'cloud_security_policy',
  'security_awareness_policy', 'employee_security_policy',
  'supplier_security_policy', 'third_party_security_policy', 'vendor_risk_management_policy',
  'incident_response_policy', 'incident_response_plan',
  'isms_manual', 'risk_management_concept', 'devsecops_policy',
  'asset_management_policy'
);

-- KMU-fokussierte IT-Security Themen (oft erst ab gewisser Groesse)
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{kmu,konzern}'
WHERE document_type IN (
  'isms_manual', 'disaster_recovery_policy', 'business_continuity_policy',
  'crisis_management_policy', 'devsecops_policy'
);

-- ============================================================
-- AI GOVERNANCE
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu,konzern}', functional_category = 'ai_governance'
WHERE document_type = 'ai_usage_policy';

-- ============================================================
-- INTERNAL POLICY
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu,konzern}', functional_category = 'internal_policy'
WHERE document_type IN (
  'whistleblower_policy', 'copyright_policy', 'community_guidelines',
  'acceptable_use', 'change_management_policy', 'remote_work_policy',
  'standard_operating_procedure'
);

-- Whistleblower-Policy: erst ab KMU (>= 50 MA) Pflicht
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{kmu,konzern}'
WHERE document_type = 'whistleblower_policy';

-- ============================================================
-- PUBLIC FACING (Website / Impressum)
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{founding,startup,kmu,konzern}', functional_category = 'public_facing'
WHERE document_type = 'impressum';

-- ============================================================
-- VENDOR / SUPPLIER
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu,konzern}', functional_category = 'vendor_supplier'
WHERE document_type IN ('supplier_security_policy','third_party_security_policy','vendor_risk_management_policy');

-- ============================================================
-- DEFAULT FALLBACK fuer unkategorisierte Templates
-- (Templates ohne lifecycle_stage erhalten Default-Set)
-- ============================================================
UPDATE compliance_legal_templates SET
  lifecycle_stage = '{startup,kmu,konzern}'
WHERE lifecycle_stage = '{}' OR lifecycle_stage IS NULL;

UPDATE compliance_legal_templates SET
  functional_category = 'internal_policy'
WHERE functional_category IS NULL;

-- ============================================================
-- VERIFIKATION
-- ============================================================
SELECT
  functional_category,
  COUNT(*) AS template_count,
  array_agg(DISTINCT unnest_stage) AS lifecycle_stages
FROM compliance_legal_templates,
LATERAL unnest(lifecycle_stage) AS unnest_stage
GROUP BY functional_category
ORDER BY functional_category;

-- Detail-Pruefung: Notartermin-Bundle
SELECT
  document_type, title, lifecycle_stage, functional_category
FROM compliance_legal_templates
WHERE 'founding' = ANY(lifecycle_stage)
   OR 'pre_founding' = ANY(lifecycle_stage)
ORDER BY functional_category, document_type;
