﻿# DDM 2.0.4 — Household Spouse bridge
#
# Reuses CK3's EXISTING councillor_spouse position rather than creating a new council type. New
# council-position types are save-sensitive; overriding the existing spouse position lets an existing
# save immediately put the ruler's one DDM-recognized household member in the vanilla spouse seat.
#
# Vanilla spouse behaviour is preserved. The only additional valid path is the ruler's currently
# recognized, active DDM Harem member. DDM never changes her marriage/concubine relationship to do this.

councillor_spouse = {
    auto_fill = yes
    can_fire = no
    can_reassign = no

    name = {
        first_valid = {
            triggered_desc = {
                trigger = { scope:councillor_liege ?= { has_variable = ddm_recognized_consort } }
                desc = DDM_COUNCILLOR_HOUSEHOLD_SPOUSE
            }
            desc = councillor_spouse
        }
    }
    tooltip = game_concept_spouse_desc

    valid_position = {
        AND = {
            NOT = { has_diarchy_active_parameter = vizier_sits_in_the_council_instead_of_spouse }
            OR = {
                # Vanilla path.
                any_spouse = {
                    OR = {
                        court_owner = root
                        AND = { is_ruler = yes exists = liege liege = root }
                    }
                }
                # DDM path. The relationship itself can remain wife, secondary spouse, native concubine,
                # lover/courtier, etc.; the Office registry and explicit recognition are what matter here.
                AND = {
                    has_variable = ddm_household_mode
                    var:ddm_household_mode = 1
                    has_variable = ddm_recognized_consort
                    var:ddm_recognized_consort = {
                        is_alive = yes
                        age >= 18
                        is_imprisoned = no
                        has_variable = ddm_harem_active
                        var:ddm_harem_active = 1
                        has_variable = ddm_harem_patron
                        var:ddm_harem_patron = root
                    }
                }
            }
        }
        NOT = { government_has_flag = government_is_landless_adventurer }
    }

    valid_character = {
        exists = root.liege_or_court_owner
        save_temporary_scope_as = ddm_spouse_candidate
        OR = {
            # Vanilla spouse/consort eligibility remains valid.
            can_be_spouse_councillor_trigger = { COUNCIL_OWNER = root.liege_or_court_owner }

            # One explicit DDM Household Spouse may use the same seat without becoming a vanilla spouse.
            AND = {
                is_alive = yes
                age >= 18
                is_imprisoned = no
                has_variable = ddm_harem_active
                var:ddm_harem_active = 1
                has_variable = ddm_harem_patron
                root.liege_or_court_owner = {
                    is_alive = yes
                    is_landed = yes
                    has_variable = ddm_household_mode
                    var:ddm_household_mode = 1
                    has_variable = ddm_recognized_consort
                    var:ddm_recognized_consort = scope:ddm_spouse_candidate
                }
            }
        }
    }

    on_get_position = {
        set_variable = { name = councillor_spouse_lifestyle_progress value = 0 }
        # The vanilla spouse-background event assumes an actual marital spouse in several branches. Keep
        # that flavour for genuine spouses, but do not manufacture marriage history for a DDM appointee.
        if = {
            limit = {
                exists = scope:councillor_liege
                is_spouse_of = scope:councillor_liege
            }
            trigger_event = councillor_spouse_background.0001
        }
    }

    on_lose_position = { remove_variable = councillor_spouse_lifestyle_progress }
    on_fired_from_position = { remove_variable = councillor_spouse_lifestyle_progress }

    barbershop_data = {
        position = { 763.0 261.00 }
        click_to_front = yes
    }
}
