-- =============================================================================
-- Zusätzliche politische Amtsvorteile (reputation_periodic, Ernennungen, Steuerkompetenz, …)
-- =============================================================================
-- Voraussetzung: falukant_political_office_benefits.sql (oder gleichwertiges Schema) ist angelegt.
-- Idempotent: mehrfach ausführbar.
--
-- Hinweis: Frische Dev-DBs bekommen dieselben Zeilen über initializePoliticalOfficeBenefits (Node).

INSERT INTO falukant_type.political_office_benefit_type (tr)
SELECT 'reputation_periodic' WHERE NOT EXISTS (SELECT 1 FROM falukant_type.political_office_benefit_type t WHERE t.tr = 'reputation_periodic');
INSERT INTO falukant_type.political_office_benefit_type (tr)
SELECT 'appoint_politicians' WHERE NOT EXISTS (SELECT 1 FROM falukant_type.political_office_benefit_type t WHERE t.tr = 'appoint_politicians');
INSERT INTO falukant_type.political_office_benefit_type (tr)
SELECT 'set_regional_tax' WHERE NOT EXISTS (SELECT 1 FROM falukant_type.political_office_benefit_type t WHERE t.tr = 'set_regional_tax');
INSERT INTO falukant_type.political_office_benefit_type (tr)
SELECT 'free_lover_slots' WHERE NOT EXISTS (SELECT 1 FROM falukant_type.political_office_benefit_type t WHERE t.tr = 'free_lover_slots');
INSERT INTO falukant_type.political_office_benefit_type (tr)
SELECT 'guard_protection' WHERE NOT EXISTS (SELECT 1 FROM falukant_type.political_office_benefit_type t WHERE t.tr = 'guard_protection');
INSERT INTO falukant_type.political_office_benefit_type (tr)
SELECT 'court_immunity' WHERE NOT EXISTS (SELECT 1 FROM falukant_type.political_office_benefit_type t WHERE t.tr = 'court_immunity');

-- reputation_periodic
INSERT INTO falukant_predefine.political_office_benefit (office_type_id, benefit_type_id, value)
SELECT ot.id, bt.id, v::jsonb FROM falukant_type.political_office_type ot
JOIN falukant_type.political_office_benefit_type bt ON bt.tr = 'reputation_periodic'
CROSS JOIN (VALUES
  ('assessor', '{"intervalDays":28,"gain":1}'::text),
  ('master-builder', '{"intervalDays":21,"gain":1}'::text),
  ('village-major', '{"intervalDays":21,"gain":1}'::text),
  ('councillor', '{"intervalDays":28,"gain":1}'::text),
  ('council', '{"intervalDays":21,"gain":1}'::text),
  ('mayor', '{"intervalDays":14,"gain":2}'::text),
  ('town-clerk', '{"intervalDays":21,"gain":1}'::text),
  ('judge', '{"intervalDays":14,"gain":2}'::text),
  ('bailif', '{"intervalDays":21,"gain":1}'::text),
  ('sheriff', '{"intervalDays":14,"gain":2}'::text),
  ('taxman', '{"intervalDays":21,"gain":2}'::text),
  ('treasurer', '{"intervalDays":14,"gain":3}'::text),
  ('consultant', '{"intervalDays":21,"gain":2}'::text),
  ('hangman', '{"intervalDays":21,"gain":1}'::text),
  ('territorial-council', '{"intervalDays":14,"gain":2}'::text),
  ('territorial-council-speaker', '{"intervalDays":10,"gain":3}'::text),
  ('ruler-consultant', '{"intervalDays":10,"gain":3}'::text),
  ('state-administrator', '{"intervalDays":10,"gain":3}'::text),
  ('super-state-administrator', '{"intervalDays":7,"gain":4}'::text),
  ('governor', '{"intervalDays":7,"gain":4}'::text),
  ('minister', '{"intervalDays":10,"gain":3}'::text),
  ('ministry-helper', '{"intervalDays":14,"gain":2}'::text),
  ('chancellor', '{"intervalDays":7,"gain":5}'::text)
) AS s(office_name, v)
WHERE ot.name = s.office_name
AND NOT EXISTS (SELECT 1 FROM falukant_predefine.political_office_benefit x WHERE x.office_type_id = ot.id AND x.benefit_type_id = bt.id);

-- appoint_politicians
INSERT INTO falukant_predefine.political_office_benefit (office_type_id, benefit_type_id, value)
SELECT ot.id, bt.id, v::jsonb FROM falukant_type.political_office_type ot
JOIN falukant_type.political_office_benefit_type bt ON bt.tr = 'appoint_politicians'
CROSS JOIN (VALUES
  ('mayor', '{"officeTrs":["beadle","town-clerk"]}'::text),
  ('judge', '{"officeTrs":["bailif"]}'::text),
  ('governor', '{"officeTrs":["state-administrator","consultant"]}'::text),
  ('super-state-administrator', '{"officeTrs":["territorial-council","hangman"]}'::text),
  ('chancellor', '{"officeTrs":["minister","ministry-helper","super-state-administrator"]}'::text)
) AS s(office_name, v)
WHERE ot.name = s.office_name
AND NOT EXISTS (SELECT 1 FROM falukant_predefine.political_office_benefit x WHERE x.office_type_id = ot.id AND x.benefit_type_id = bt.id);

-- set_regional_tax
INSERT INTO falukant_predefine.political_office_benefit (office_type_id, benefit_type_id, value)
SELECT ot.id, bt.id, v::jsonb FROM falukant_type.political_office_type ot
JOIN falukant_type.political_office_benefit_type bt ON bt.tr = 'set_regional_tax'
CROSS JOIN (VALUES
  ('taxman', '{"scope":"local"}'::text),
  ('treasurer', '{"scope":"shire"}'::text),
  ('super-state-administrator', '{"scope":"duchy"}'::text),
  ('chancellor', '{"scope":"national"}'::text)
) AS s(office_name, v)
WHERE ot.name = s.office_name
AND NOT EXISTS (SELECT 1 FROM falukant_predefine.political_office_benefit x WHERE x.office_type_id = ot.id AND x.benefit_type_id = bt.id);

-- free_lover_slots
INSERT INTO falukant_predefine.political_office_benefit (office_type_id, benefit_type_id, value)
SELECT ot.id, bt.id, v::jsonb FROM falukant_type.political_office_type ot
JOIN falukant_type.political_office_benefit_type bt ON bt.tr = 'free_lover_slots'
CROSS JOIN (VALUES
  ('councillor', '{"count":1}'::text),
  ('mayor', '{"count":1}'::text),
  ('minister', '{"count":1}'::text),
  ('governor', '{"count":2}'::text),
  ('chancellor', '{"count":3}'::text)
) AS s(office_name, v)
WHERE ot.name = s.office_name
AND NOT EXISTS (SELECT 1 FROM falukant_predefine.political_office_benefit x WHERE x.office_type_id = ot.id AND x.benefit_type_id = bt.id);

-- guard_protection / court_immunity
INSERT INTO falukant_predefine.political_office_benefit (office_type_id, benefit_type_id, value)
SELECT ot.id, bt.id, '{}'::jsonb FROM falukant_type.political_office_type ot
JOIN falukant_type.political_office_benefit_type bt ON bt.tr = 'guard_protection'
CROSS JOIN (VALUES ('sheriff'), ('hangman'), ('minister')) AS s(office_name)
WHERE ot.name = s.office_name
AND NOT EXISTS (SELECT 1 FROM falukant_predefine.political_office_benefit x WHERE x.office_type_id = ot.id AND x.benefit_type_id = bt.id);

INSERT INTO falukant_predefine.political_office_benefit (office_type_id, benefit_type_id, value)
SELECT ot.id, bt.id, '{}'::jsonb FROM falukant_type.political_office_type ot
JOIN falukant_type.political_office_benefit_type bt ON bt.tr = 'court_immunity'
CROSS JOIN (VALUES ('judge'), ('councillor'), ('chancellor')) AS s(office_name)
WHERE ot.name = s.office_name
AND NOT EXISTS (SELECT 1 FROM falukant_predefine.political_office_benefit x WHERE x.office_type_id = ot.id AND x.benefit_type_id = bt.id);
