-- PostgreSQL-only backfill script.
-- Dieses Projekt-Backend nutzt Schemas und PostgreSQL-spezifische SQL-Strukturen.
-- Nicht auf MariaDB/MySQL ausführen.

BEGIN;

INSERT INTO falukant_data.relationship_state (
  relationship_id,
  marriage_satisfaction,
  marriage_public_stability,
  lover_role,
  affection,
  visibility,
  discretion,
  maintenance_level,
  status_fit,
  monthly_base_cost,
  months_underfunded,
  active,
  acknowledged,
  exclusive_flag,
  created_at,
  updated_at
)
SELECT
  r.id,
  55,
  55,
  CASE WHEN rt.tr = 'lover' THEN 'lover' ELSE NULL END,
  50,
  CASE WHEN rt.tr = 'lover' THEN 20 ELSE 15 END,
  CASE WHEN rt.tr = 'lover' THEN 45 ELSE 50 END,
  50,
  0,
  CASE WHEN rt.tr = 'lover' THEN 30 ELSE 0 END,
  0,
  true,
  false,
  false,
  CURRENT_TIMESTAMP,
  CURRENT_TIMESTAMP
FROM falukant_data.relationship r
INNER JOIN falukant_type.relationship rt
  ON rt.id = r.relationship_type_id
LEFT JOIN falukant_data.relationship_state rs
  ON rs.relationship_id = r.id
WHERE rs.id IS NULL
  AND rt.tr IN ('lover', 'wooing', 'engaged', 'married');

COMMIT;
