﻿
change_to_administrative_effect = {
	save_scope_as = governor
	top_liege = { save_scope_as = governor_liege }
	if = {
		limit = {
			OR = {
				NOT = { government_allows = administrative }
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_japan_administrative }
					NOT = { government_has_flag = government_is_japan_administrative }
				}
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_meritocratic }
					NOT = { government_has_flag = government_is_meritocratic }
				}
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_celestial }
					NOT = { government_has_flag = government_is_celestial }
				}
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_steppe_admin }
					NOT = { government_has_flag = government_is_steppe_admin }
				}
			}
		}
		# Only case where Estate type is shared between Admin and non-Admin
		if = {
			limit = {
				NAND = {
					OR = {
						government_has_flag = government_is_japan_feudal
						government_has_flag = government_is_feudal
					}
					scope:governor_liege = { government_has_flag = government_is_japan_administrative }
				}
			}
			save_scope_as = new_admin
		}
		# Change government
		if = {
			limit = {
				scope:governor_liege = { government_has_flag = government_is_japan_administrative }
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = japanese_manor
				NEW_GOVERNMENT_TYPE = japan_administrative_government
			}
			change_government = japan_administrative_government
		}
		else_if = {
			limit = {
				OR = {
					#This is Mongol breakup and they are hegemon
					AND = {
						exists = scope:great_yuan_ruler
						this = scope:great_yuan_ruler
						primary_title = title:h_china
					}
					scope:governor_liege = { government_has_flag = government_is_celestial }
				}
			}
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = east_asian_estate
				NEW_GOVERNMENT_TYPE = celestial_government
			}
			change_government = celestial_government
		}
		else_if = {
			limit = {
				scope:governor_liege = { government_has_flag = government_is_steppe_admin }
			}
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = east_asian_estate
				NEW_GOVERNMENT_TYPE = steppe_admin_government
			}
			change_government = steppe_admin_government
		}
		else_if = {
			limit = {
				OR = {
					scope:governor_liege = { government_has_flag = government_is_meritocratic }
					tgp_should_become_meritocratic_trigger = yes
				}
			}
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = east_asian_estate
				NEW_GOVERNMENT_TYPE = meritocratic_government
			}
			change_government = meritocratic_government
		}
		else = {
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			add_character_flag = new_government_is_byz_admin
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = estate
				NEW_GOVERNMENT_TYPE = administrative_government
			}
			change_government = administrative_government 
			remove_character_flag = new_government_is_byz_admin
		}
	}
	if = {
		limit = {
			primary_title.tier >= min_appointment_tier
			liege = {
				scope:governor_liege = this
				government_allows = administrative
			}
			house.house_head = {
				top_liege = scope:governor_liege
				NOT = {
					any_held_title = { is_noble_family_title = yes }
				}
			}
		}
		create_noble_family_effect = { GOVERNMENT_GIVER = top_liege }
	}
	hidden_effect = {
		if = {
			limit = {
				is_governor = yes
				NOT = { has_trait = governor }
			}
			add_trait = governor
		}
	}
	if = {
		limit = {
			exists = scope:new_admin
			exists = domicile
		}
		domicile ?= {
			set_up_domicile_estate_effect = yes
		}
	}
	if = {
		limit = {
			has_treasury = yes
		}
		add_treasury = {
			value = gold
			min = 100
		}
	}
}
