# Given a species, create a mod with trait_cybernetic and apply to all pops, leaders
# and armies that have the original species and the same owner as the scope object.
# Scope: Something that has a species and an owner
create_cybernetic_species_mod = {
	log = "in override effect"
	if = {
		limit = {
			owner = {
				aap_can_psionic_assimilate = yes
				has_tradition = tr_cybernetics_adopt
			}
		}
		species = {
			modify_species = {
				remove_trait = trait_latent_psionic
				effect = {
					create_species = {
						is_mod = yes
						name = this
						plural = this
						class = this
						portrait = this
						traits = this
						can_be_modified = this
						homeworld = this
						namelist = this
						gender = this
						traits = {
							ideal_planet_class = this
							trait = trait_cybernetic
							trait = trait_psionic
						}
					}
				} 
			}
			save_event_target_as = changing_species
		}
	}
	else_if = {
		limit = {
			owner = {
				aap_can_psionic_assimilate = yes
			}
		}
		species = {
			modify_species = {
				remove_trait = trait_latent_psionic
				effect = {
					create_species = {
						is_mod = yes
						name = this
						plural = this
						class = this
						portrait = this
						traits = this
						can_be_modified = this
						homeworld = this
						namelist = this
						gender = this
						traits = {
							ideal_planet_class = this
							trait = trait_psionic
						}
					}
				}
			}
			save_event_target_as = changing_species
		}
	}
	else = {
		species = {
			modify_species = {
				remove_trait = trait_latent_psionic
				remove_trait = trait_psionic
				effect = {
					create_species = {
						is_mod = yes
						name = this
						plural = this
						class = this
						portrait = this
						traits = this
						can_be_modified = this
						homeworld = this
						namelist = this
						gender = this
						traits = {
							ideal_planet_class = this
							trait = trait_cybernetic
						}
					}
				}
			}
			save_event_target_as = changing_species
		}
	}

	owner = {
		every_owned_pop_group = {
			limit = { is_exact_same_species = event_target:changing_species }
			change_species = last_created_species
		}
		every_owned_leader = {
			limit = { is_exact_same_species = event_target:changing_species }
			change_species = last_created_species
		}
		every_pool_leader = {
			limit = { is_exact_same_species = event_target:changing_species }
			change_species = last_created_species
		}
		every_envoy = {
			limit = { is_exact_same_species = event_target:changing_species }
			change_species = last_created_species
		}
		every_owned_army = {
			limit = {
				exists = species
				is_exact_same_species = event_target:changing_species
			}
			change_species = last_created_species
		}
		every_controlled_ship = {
			limit = {
				is_ship_class = shipclass_colonizer
				is_exact_same_species = event_target:changing_species
			}
			change_species = last_created_species
		}
	}
}


# Leader scope
update_leader_after_modification = {
    if = {
        limit = {
            is_leader_tier = leader_tier_default
            has_any_chosen_one_leader_trait = no
        }

        # Remove all applicable leader traits
        # remove_trait = leader_trait_erudite
        # remove_trait = leader_trait_cyborg
        # remove_trait = leader_trait_limited_cyborg
        # remove_trait = leader_trait_firewalled
        # remove_trait = leader_trait_hacked
        # remove_trait = leader_trait_psionic
        # remove_trait = leader_trait_virtual
        # remove_trait = leader_trait_synthetic

        # Add the correct leader trait based on the species
        if = {
            limit = {
                is_robotic_species = no
            }
            species = {
                switch = {
                    trigger = has_trait
                    trait_erudite = {
                        prev = {
                            add_trait = {
                                trait = leader_trait_erudite
                                show_message = no
                            }
                        }
                    }
					trait_cybernetic = {
						prev = {
							if = {
								limit = {
									is_spiritualist = yes
									owner = {
										has_policy_flag = cyberization_standards_limited
									}
								}
								add_trait = {
									trait = leader_trait_limited_cyborg
									show_message = no
								}
							}
							else = {
								add_trait = {
									trait = leader_trait_cyborg
									show_message = no
								}
							}
							if = {
								limit = {
									owner = {
										has_country_flag = firewall_built
									}
								}
								add_trait = {
									trait = leader_trait_firewalled
									show_message = no
								}
							}
						}
					}
                    trait_psionic = {
                        prev = {
                            add_trait = {
                                trait = leader_trait_psionic
                                show_message = no
                            }
                        }
                    }
                }
            }
        }
        if = {
            limit = {
                species = {
                    has_trait = trait_robot_psionic
                }
            }
            add_trait = {
                trait = leader_trait_psionic
                show_message = no
            }
        }
        else_if = {
            limit = {
                species = {
                    has_virtual_species_trait = yes
                }
            }
            add_trait = {
                trait = leader_trait_virtual
                show_message = no
            }
        }
        else = {
            if = {
				limit = {
					owner = { has_country_flag = driven_cybernetics }
				}
				add_trait = {
					trait = leader_trait_cyborg
					show_message = no
				}
			}
			else_if = {
                limit = {
                    owner = {
                        OR = {
							has_country_flag = synthetic_empire
                            has_tradition = tr_synthetics_finish
                            has_tradition = tr_modularity_finish
                        }
                    }
                }
                add_trait = {
                    trait = leader_trait_synthetic
                    show_message = no
                }
            }
        }
    }
}