namespace = qyv_ai_intrigue

# ============================================================
# MONTHLY: SPY NETWORK BUILDING & SPYMASTER MANAGEMENT
# ============================================================

character_event = {
    id = qyv_intrigue.0100
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        # Assign spymaster if vacant
        if = {
            limit = { qyv_ai_should_assign_spymaster = root }
            qyv_ai_assign_spymaster = yes
        }
        # Replace incompetent spymaster
        if = {
            limit = { qyv_ai_should_replace_spymaster = root }
            qyv_ai_replace_spymaster = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# MONTHLY: BUILD SPY NETWORKS
# ============================================================

character_event = {
    id = qyv_intrigue.0200
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        intrigue >= 8
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        # Build spy networks on priority targets
        if = {
            limit = { qyv_ai_should_build_spy_network = root }
            qyv_ai_build_spy_network = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# MONTHLY: EXECUTE SCHEMES (with spy network ready)
# ============================================================

character_event = {
    id = qyv_intrigue.0300
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 1 }

    immediate = {
        # Murder scheme (requires high spy network)
        if = {
            limit = { qyv_ai_should_scheme_murder = root }
            qyv_ai_scheme_murder = yes
        }

        # Abduct heir
        if = {
            limit = { qyv_ai_should_scheme_abduct = root }
            qyv_ai_scheme_abduct = yes
        }

        # Fabricate hook on vassal
        if = {
            limit = { qyv_ai_should_scheme_hook = root }
            qyv_ai_scheme_hook = yes
        }

        # Fabricate claim
        if = {
            limit = { qyv_ai_should_scheme_claim = root }
            qyv_ai_scheme_claim = yes
        }

        # Support antiking claimant
        if = {
            limit = { qyv_ai_should_scheme_antiking = root }
            qyv_ai_scheme_antiking = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# MONTHLY: SECRET EXPLOITATION
# ============================================================

character_event = {
    id = qyv_intrigue.0400
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
        intrigue >= 10
    }

    mean_time_to_happen = { months = 2 }

    immediate = {
        # Use secrets for blackmail
        if = {
            limit = { qyv_ai_should_use_secrets = root }
            qyv_ai_use_secrets = yes
        }

        # Blackmail for gold/council
        if = {
            limit = { qyv_ai_should_blackmail = root }
            qyv_ai_blackmail = yes
        }

        # Expose rival's secret
        if = {
            limit = { qyv_ai_should_expose_secret = root }
            qyv_ai_expose_secret = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# QUARTERLY: HOOK USAGE ON VASSALS
# ============================================================

character_event = {
    id = qyv_intrigue.0500
    hidden = yes
    is_triggered_only = no

    trigger = {
        is_ai = yes
        is_ruler = yes
    }

    mean_time_to_happen = { months = 3 }

    immediate = {
        # Use hook on powerful vassal for council
        if = {
            limit = { qyv_ai_can_force_vassalization_via_hook = root }
            qyv_ai_use_hook_on_vassal = yes
        }

        # Use hook for gold
        if = {
            limit = { qyv_ai_has_hook_on_rival = root }
            qyv_ai_use_hook_for_gold = yes
        }

        # Use hook for alliance
        if = {
            limit = { qyv_ai_has_hook_on_rival = root }
            qyv_ai_use_hook_for_alliance = yes
        }

        # Use hook for marriage
        if = {
            limit = { qyv_ai_has_hook_on_rival = root }
            qyv_ai_use_hook_for_marriage = yes
        }
    }

    option = { name = "OK" }
}

# ============================================================
# ON_SCHEME_END HOOKS - Chain Reactions
# ============================================================

on_actions = {
    # Monthly pulses
    on_monthly_pulse = { events = { qyv_intrigue.0100 qyv_intrigue.0200 qyv_intrigue.0300 qyv_intrigue.0400 } }

    # Quarterly hook usage
    on_quarterly_pulse = { events = { qyv_intrigue.0500 } }

    # Scheme completion chains
    on_scheme_end = {
        # Murder success -> disinherit if heir
        trigger_if = {
            limit = {
                scheme = scheme_murder
                outcome = success
                target = { is_heir = yes }
            }
            target = { disinherit = yes }
        }

        # Hook fabricated on vassal -> enforce for council
        trigger_if = {
            limit = {
                scheme = scheme_fabricate_hook
                outcome = success
                target = { is_vassal_of = root }
                target = { is_powerful = yes }
            }
            root = {
                enforce_hook = {
                    target = scope:target
                    demand_council_position = yes
                }
            }
        }

        # Hook fabricated -> demand gold
        trigger_if = {
            limit = {
                scheme = scheme_fabricate_hook
                outcome = success
                target = { is_rival_of = root }
            }
            root = {
                enforce_hook = {
                    target = scope:target
                    demand_gold = 300
                }
            }
        }

        # Claim fabricated -> declare war if adjacent
        trigger_if = {
            limit = {
                scheme = scheme_fabricate_claim
                outcome = success
                target = { has_claim_on = root }
                NOT = { is_at_war = yes }
                gold >= 500
                any_owned_province = {
                    any_neighbor_province = {
                        owner = { is_ruler = scope:target }
                    }
                }
            }
            declare_war = { target = scope:target cb = claim }
        }

        # Hook on powerful vassal -> demand council
        trigger_if = {
            limit = {
                scheme = scheme_fabricate_hook
                outcome = success
                target = { is_vassal_of = root }
                target = { is_powerful = yes }
            }
            root = {
                enforce_hook = {
                    target = scope:target
                    demand_council_position = yes
                }
            }
        }

        # Abduct heir -> force marriage/alliance
        trigger_if = {
            limit = {
                scheme = scheme_abduct
                outcome = success
                target = { is_heir = yes }
            }
            root = {
                enforce_hook = {
                    target = scope:target
                    demand_marriage = { spouse = root }
                }
            }
        }
    }
}