-- PostgreSQL only
-- Ersetzt die Standesanforderungen fuer Falukant durch das erweiterte Profilmodell.

DELETE FROM falukant_type.title_requirement
WHERE title_id IN (
    SELECT id
    FROM falukant_type.title
    WHERE label_tr IN (
        'civil', 'sir', 'townlord', 'by', 'landlord', 'knight',
        'baron', 'count', 'palsgrave', 'margrave', 'landgrave',
        'ruler', 'elector', 'imperial-prince', 'duke', 'grand-duke',
        'prince-regent', 'king'
    )
);

INSERT INTO falukant_type.title_requirement (title_id, requirement_type, requirement_value)
SELECT tm.id, req.requirement_type, req.requirement_value
FROM (
    SELECT id, label_tr
    FROM falukant_type.title
    WHERE label_tr IN (
        'civil', 'sir', 'townlord', 'by', 'landlord', 'knight',
        'baron', 'count', 'palsgrave', 'margrave', 'landgrave',
        'ruler', 'elector', 'imperial-prince', 'duke', 'grand-duke',
        'prince-regent', 'king'
    )
) tm
JOIN (
    VALUES
        ('civil', 'money', 5000::numeric),
        ('civil', 'cost', 500::numeric),
        ('civil', 'house_position', 2::numeric),

        ('sir', 'branches', 2::numeric),
        ('sir', 'cost', 1000::numeric),
        ('sir', 'house_position', 3::numeric),

        ('townlord', 'cost', 3000::numeric),
        ('townlord', 'money', 12000::numeric),
        ('townlord', 'reputation', 18::numeric),
        ('townlord', 'house_position', 4::numeric),

        ('by', 'cost', 5000::numeric),
        ('by', 'money', 18000::numeric),
        ('by', 'house_position', 4::numeric),

        ('landlord', 'cost', 7500::numeric),
        ('landlord', 'money', 26000::numeric),
        ('landlord', 'reputation', 24::numeric),
        ('landlord', 'house_position', 4::numeric),
        ('landlord', 'house_condition', 60::numeric),

        ('knight', 'cost', 11000::numeric),
        ('knight', 'money', 38000::numeric),
        ('knight', 'office_rank_any', 1::numeric),
        ('knight', 'house_position', 5::numeric),

        ('baron', 'branches', 4::numeric),
        ('baron', 'cost', 16000::numeric),
        ('baron', 'money', 55000::numeric),
        ('baron', 'house_position', 5::numeric),

        ('count', 'cost', 23000::numeric),
        ('count', 'money', 80000::numeric),
        ('count', 'reputation', 32::numeric),
        ('count', 'house_position', 5::numeric),
        ('count', 'house_condition', 68::numeric),

        ('palsgrave', 'cost', 32000::numeric),
        ('palsgrave', 'money', 115000::numeric),
        ('palsgrave', 'office_rank_any', 2::numeric),
        ('palsgrave', 'house_position', 6::numeric),

        ('margrave', 'cost', 45000::numeric),
        ('margrave', 'money', 165000::numeric),
        ('margrave', 'reputation', 40::numeric),
        ('margrave', 'house_position', 6::numeric),
        ('margrave', 'house_condition', 72::numeric),
        ('margrave', 'lover_count_min', 1::numeric),

        ('landgrave', 'cost', 62000::numeric),
        ('landgrave', 'money', 230000::numeric),
        ('landgrave', 'office_rank_any', 3::numeric),
        ('landgrave', 'house_position', 6::numeric),

        ('ruler', 'cost', 85000::numeric),
        ('ruler', 'money', 320000::numeric),
        ('ruler', 'reputation', 48::numeric),
        ('ruler', 'house_position', 7::numeric),
        ('ruler', 'house_condition', 76::numeric),

        ('elector', 'cost', 115000::numeric),
        ('elector', 'money', 440000::numeric),
        ('elector', 'office_rank_political', 4::numeric),
        ('elector', 'house_position', 7::numeric),
        ('elector', 'lover_count_max', 3::numeric),

        ('imperial-prince', 'cost', 155000::numeric),
        ('imperial-prince', 'money', 600000::numeric),
        ('imperial-prince', 'reputation', 56::numeric),
        ('imperial-prince', 'house_position', 7::numeric),
        ('imperial-prince', 'house_condition', 80::numeric),

        ('duke', 'cost', 205000::numeric),
        ('duke', 'money', 820000::numeric),
        ('duke', 'office_rank_any', 5::numeric),
        ('duke', 'house_position', 8::numeric),

        ('grand-duke', 'cost', 270000::numeric),
        ('grand-duke', 'money', 1120000::numeric),
        ('grand-duke', 'reputation', 64::numeric),
        ('grand-duke', 'house_position', 8::numeric),
        ('grand-duke', 'house_condition', 84::numeric),
        ('grand-duke', 'lover_count_min', 1::numeric),
        ('grand-duke', 'lover_count_max', 3::numeric),

        ('prince-regent', 'cost', 360000::numeric),
        ('prince-regent', 'money', 1520000::numeric),
        ('prince-regent', 'office_rank_any', 6::numeric),
        ('prince-regent', 'house_position', 9::numeric),

        ('king', 'cost', 500000::numeric),
        ('king', 'money', 2100000::numeric),
        ('king', 'reputation', 72::numeric),
        ('king', 'house_position', 9::numeric),
        ('king', 'house_condition', 88::numeric),
        ('king', 'lover_count_min', 1::numeric),
        ('king', 'lover_count_max', 4::numeric)
) AS req(label_tr, requirement_type, requirement_value)
    ON req.label_tr = tm.label_tr
ON CONFLICT (title_id, requirement_type)
DO UPDATE SET requirement_value = EXCLUDED.requirement_value;
