# ============================================================
# ARTIFACT MANAGEMENT EFFECTS
# ============================================================

# Artifact Equipping
qyv_ai_equip_best_artifact = {
    any_equipped_artifact = {
        limit = {
            any_artifact = {
                is_equippable = yes
                owner = root
                slot = equipped_artifact.slot
                quality > equipped_artifact.quality
            }
        }
        unequip_artifact = equipped_artifact
    }
    any_artifact = {
        limit = {
            is_equippable = yes
            owner = root
            NOT = { equipped_by = root }
            quality > 0
        }
        equip_artifact = this
    }
}

qyv_ai_equip_heir = {
    any_child = {
        limit = { is_heir = yes is_adult = yes }
        any_artifact = {
            limit = {
                is_equippable = yes
                owner = root
                NOT = { equipped_by = root }
                quality > 0
            }
            equip_artifact = { target = scope:heir artifact = this }
        }
    }
}

qyv_ai_equip_knight = {
    any_knight = {
        limit = { prowess >= 15 }
        any_artifact = {
            limit = {
                is_equippable = yes
                owner = root
                NOT = { equipped_by = root }
                slot = weapon
                quality >= 2
            }
            equip_artifact = { target = scope:knight artifact = this }
        }
    }
}

qyv_ai_equip_for_duel = {
    any_artifact = {
        limit = {
            is_equippable = yes
            owner = root
            quality >= 3
            slot = weapon
        }
        equip_artifact = this
    }
}

# Artifact Acquisition
qyv_ai_hunt_artifact = {
    any_artifact = {
        limit = {
            is_equippable = yes
            NOT = { owner = root }
            quality >= 3
            owner = {
                is_ruler = yes
                NOT = { is_allied_to = root }
                military_strength < root.military_strength * 1.5
            }
        }
        trigger_event = { id = qyv_artifact.0100 days = 1 scope:target = this }
        set_character_flag = artifact_hunt_cooldown
    }
}

qyv_ai_forge_artifact = {
    any_courtier = {
        limit = {
            is_artificer = yes
            skill >= 10
        }
        start_scheme = { scheme = scheme_forge_artifact target = root }
    }
    set_character_flag = artifact_forge_cooldown
}

qyv_ai_steal_artifact = {
    any_artifact = {
        limit = {
            is_equippable = yes
            NOT = { owner = root }
            quality >= 3
            owner = {
                is_ruler = yes
                NOT = { is_allied_to = root }
                NOT = { has_truce_with = root }
            }
        }
        start_scheme = { scheme = scheme_steal_artifact target = owner artifact = this }
    }
}

qyv_ai_gift_artifact = {
    scope:gift_target = {
        add_artifact = this
        add_opinion = { target = root modifier = artifact_gift years = 5 }
    }
    root = {
        remove_artifact = this
    }
}

qyv_ai_equip_best = {
    any_equipped_artifact = {
        limit = {
            any_artifact = {
                is_equippable = yes
                owner = root
                slot = equipped_artifact.slot
                quality > equipped_artifact.quality
            }
        }
        unequip_artifact = equipped_artifact
    }
    any_artifact = {
        limit = {
            is_equippable = yes
            owner = root
            NOT = { equipped_by = root }
            slot = equipped_artifact.slot
            quality > 0
        }
        equip_artifact = this
    }
}

qyv_ai_equip_heir_artifact = {
    any_child = {
        limit = { is_heir = yes is_adult = yes }
        any_artifact = {
            limit = {
                is_equippable = yes
                owner = root
                NOT = { equipped_by = root }
                slot = any_equipped_artifact.slot
                quality > any_equipped_artifact.quality
            }
            equip_artifact = { target = scope:heir artifact = this }
        }
    }
}

qyv_ai_claim_artifact = {
    any_artifact = {
        limit = {
            is_equippable = yes
            quality >= 3
            has_claimant = root
            owner = {
                is_ruler = yes
                NOT = { is_allied_to = root }
            }
        }
        trigger_event = { id = qyv_artifact.0200 days = 1 scope:target = this }
    }
}

qyv_ai_forge_specific = {
    any_courtier = {
        limit = { is_artificer = yes skill >= 10 }
        start_scheme = { scheme = scheme_forge_artifact target = root }
        add_character_modifier = { modifier = focused_forging years = 1 }
    }
}

qyv_ai_equip_knight_artifact = {
    any_knight = {
        limit = { prowess >= 15 }
        any_artifact = {
            limit = {
                is_equippable = yes
                owner = root
                NOT = { equipped_by = root }
                slot = weapon
                quality >= 2
            }
            equip_artifact = { target = scope:knight artifact = this }
        }
    }
}

qyv_ai_prepare_duel_artifact = {
    any_artifact = {
        limit = {
            is_equippable = yes
            owner = root
            quality >= 3
            slot = weapon
        }
        equip_artifact = this
    }
}

qyv_ai_ransom_artifact = {
    any_artifact = {
        limit = {
            is_equippable = yes
            owner = root
            NOT = { equipped_by = root }
            quality <= 2
        }
        remove_artifact = this
        add_gold = 200
    }
}

# Cooldown Management
qyv_ai_clear_artifact_cooldown = {
    remove_character_flag = artifact_hunt_cooldown
    remove_character_flag = artifact_forge_cooldown
}