﻿customs_union = {
	requires_approval = yes
	can_use_obligations = yes

	unlocking_technologies = {
	#	international_trade
	}

	potential = {
		NOR = {
			is_country_type = decentralized 
			has_law = law_type:law_isolationism
		}
		scope:target_country = {
			NOR = { 
				is_country_type = decentralized 
				has_law = law_type:law_isolationism
			}
		}
	}

	possible = {
		NOT = { has_war_with = scope:target_country }
		NOT = { is_subject_of = scope:target_country }
		is_junior_in_customs_union = no
		has_diplomatic_relevance = scope:target_country
		NOR = {
			scope:target_country = { has_law = law_type:law_isolationism }
			has_law = law_type:law_isolationism					
		}		
		custom_tooltip = {
			text = DIPLO_ACTION_RELATIONS_CORDIAL_OR_GREATER
			scope:target_country.relations:root >= relations_threshold:cordial
		}
		is_revolutionary = no
		scope:target_country = {
			is_revolutionary = no
		}
		custom_tooltip = {
			text = DIPLO_ACTION_MARKET_CONNECTION_REQUIRED
			OR = {	
				AND = {
					market = { has_port = yes }
					scope:target_country.market = { has_port = yes }
				}
				market = {
					any_scope_country = {
						is_adjacent = scope:target_country
					}
				}
			}
		}			
	}

	accept_effect = {
		if = {
			limit = {
				has_diplomatic_pact = {
					who = scope:target_country
					type = trade_agreement
				}			
			}
			remove_diplomatic_pact = {
				country = scope:target_country
				type = trade_agreement
			}			
		}
	}

	pact = {
		cost = 25
		has_junior_participant = yes
		recipient_pays_maintenance = no
		is_customs_union = yes
		
		relations_progress_per_day = 1  # At 100, +1 relations
		relations_improvement_max = 80		
		
		subject_type = subject_type_customs_union # A customs union is technically a type of subject, though it isn't presented this way in-game

		is_about_to_auto_break = {
		    scope:target_country.relations:root < 23 # this is: cordial + 3
		}

		should_auto_break = {
			custom_tooltip = {
				text = DIPLO_ACTION_RELATIONS_CORDIAL_OR_GREATER_NOT

				# make sure to update is_about_to_auto_break when changing this:
				scope:target_country.relations:root < relations_threshold:cordial
			}
		}

		should_invalidate = {
			OR = {
				has_war_with = scope:target_country
				is_country_type = decentralized
				scope:target_country = { is_country_type = decentralized }
				is_subject_of = scope:target_country
				scope:target_country = { is_subject_of = root }
				is_junior_in_customs_union = yes
				scope:target_country = { has_law = law_type:law_isolationism }
				has_law = law_type:law_isolationism		
			}
		}

		break_effect = {
			change_relations = { country = scope:target_country value = -30 }
			
			# AI may not take leaving their customs union well...
			hidden_effect = {
				if = {
					limit = { 
						scope:target_country.country_rank > root.country_rank
						"relations(scope:target_country)" < relations_threshold:cordial
					}	
					random_list = {
						75 = {
							scope:target_country = {
								set_secret_goal = {
									country = root
									secret_goal = dominate
								}
							}						
						}
						25 = {}
					}
				}
				else_if = {
					limit = { 
						scope:target_country.country_rank > root.country_rank
						"relations(scope:target_country)" < relations_threshold:friendly
					}	
					random_list = {
						50 = {
							scope:target_country = {
								set_secret_goal = {
									country = root
									secret_goal = dominate
								}
							}						
						}
						50 = {}
					}
				}					
			}
		}
	}

	ai = {
		check_acceptance_for_will_break = yes
		check_acceptance_for_will_propose = yes

		# This is the acceptance score for the senior part of the customs union (used for 'join customs union' action and checking )
		accept_score = {
			value = 0

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_BASE"
				value = -100
			}
			
			if = {
				limit = {
					NOR = {
						scope:actor.market = {
							any_scope_country = {
								is_adjacent = root
							}
						}
						scope:actor = {
							has_diplomatic_pact = { who = root type = customs_union }
						}
					}
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_NOT_ADJACENT_MARKETS"
					value = -20
				}
			}			

			if = {
				limit = { 
					has_strategy = ai_strategy_economic_imperialism
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_ECONOMIC_IMPERIALISM"
					add = 50
				}
			}	

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_SENIOR_PART"
				value = 20
			}

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_GDP"
				value = "scope:actor.ai_gdp_comparison(root)"
				min = -10
				max = 10
			}
		
			if = {
				limit = {
					"scope:actor.ai_ideological_opinion(root)" > 0
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_IDEOLOGICAL_OPINION"
					value = "scope:actor.ai_ideological_opinion(root)"
					multiply = 0.5
					max = 20
				}					
			}
			else_if = {
				limit = {
					"scope:actor.ai_ideological_opinion(root)" < 0
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_IDEOLOGICAL_OPINION_NEGATIVE"
					value = "scope:actor.ai_ideological_opinion(root)"
					multiply = 0.5
					min = -20
				}					
			}	
			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_ATTITUDE"
				if = {
					limit = {
						OR = {
							has_attitude = {
								who = scope:actor
								attitude = protective
							}
							has_attitude = {
								who = scope:actor
								attitude = domineering
							}							
						}
					}
					value = 40
				}
				if = {
					limit = {
						has_attitude = {
							who = scope:actor
							attitude = genial
						}
					}
					value = 30
				}				
				if = {
					limit = {
						OR = {
							has_attitude = {
								who = scope:actor
								attitude = conciliatory
							}	
							has_attitude = {
								who = scope:actor
								attitude = cooperative
							}							
						}
					}
					value = 20
				}
				if = {
					limit = {
						has_attitude = {
							who = scope:actor
							attitude = cautious
						}
					}
					value = -50
				}
				if = {
					limit = {
						OR = {
							has_attitude = {
								who = scope:actor
								attitude = antagonistic
							}
							has_attitude = {
								who = scope:actor
								attitude = belligerent
							}
							has_attitude = {
								who = scope:actor
								attitude = rebellious
							}
						}
					}
					value = -1000
				}
			}

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_RELATIONS"
				if = {
					limit = {
						"relations(scope:actor)" >= relations_threshold:amicable
						"relations(scope:actor)" < relations_threshold:friendly
					}
					value = 20
				}
				if = {
					limit = {
						"relations(scope:actor)" >= relations_threshold:friendly
					}
					value = 50
				}
			}

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_INFAMY"
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:infamous
						"scope:actor.infamy" < infamy_threshold:notorious
					}
					value = -10
				}
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:notorious
						"scope:actor.infamy" < infamy_threshold:pariah
					}
					value = -25
				}
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:pariah
					}
					value = -50
				}
			}
		}

		# This is the acceptance score for the junior part of the customs union
		junior_accept_score = {
			value = 0

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_BASE"
				value = -100
			}

			if = {
				limit = {
					NOR = {
						scope:actor.market = {
							any_scope_country = {
								is_adjacent = root
							}
						}
						scope:actor = {
							has_diplomatic_pact = { who = root type = customs_union }
						}
					}
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_NOT_ADJACENT_MARKETS"
					value = -20
				}
			}

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_GDP"
				value = "scope:actor.ai_gdp_comparison(root)"
				multiply = 2
				min = -20
				max = 20
			}
			
			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_MUTUAL_TRADE_ROUTES"
				value = "scope:actor.num_mutual_trade_route_levels_with_country(root)"
				multiply = 0.5
				max = 25
			}				

			if = {
				limit = {
					"scope:actor.ai_ideological_opinion(root)" > 0
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_IDEOLOGICAL_OPINION"
					value = "scope:actor.ai_ideological_opinion(root)"
					multiply = 0.5
					max = 20
				}					
			}
			else_if = {
				limit = {
					"scope:actor.ai_ideological_opinion(root)" < 0
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_IDEOLOGICAL_OPINION_NEGATIVE"
					value = "scope:actor.ai_ideological_opinion(root)"
					multiply = 0.5
					min = -20
				}					
			}		

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_ATTITUDE"
				if = {
					limit = {
						has_attitude = {
							who = scope:actor
							attitude = loyal
						}
					}
					value = 40
				}
				if = {
					limit = {
						has_attitude = {
							who = scope:actor
							attitude = genial
						}
					}
					value = 30
				}				
				if = {
					limit = {
						OR = {
							has_attitude = {
								who = scope:actor
								attitude = conciliatory
							}	
							has_attitude = {
								who = scope:actor
								attitude = cooperative
							}							
						}
					}
					value = 20
				}
				if = {
					limit = {
						has_attitude = {
							who = scope:actor
							attitude = cautious
						}
					}
					value = -50
				}
				if = {
					limit = {
						OR = {
							has_attitude = {
								who = scope:actor
								attitude = protective
							}							
							has_attitude = {
								who = scope:actor
								attitude = antagonistic
							}
							has_attitude = {
								who = scope:actor
								attitude = domineering
							}
							has_attitude = {
								who = scope:actor
								attitude = belligerent
							}
							has_attitude = {
								who = scope:actor
								attitude = rebellious
							}
						}
					}
					value = -1000
				}
			}

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_RELATIONS"
				if = {
					limit = {
						"relations(scope:actor)" >= relations_threshold:amicable
						"relations(scope:actor)" < relations_threshold:friendly
					}
					value = 20
				}
				if = {
					limit = {
						"relations(scope:actor)" >= relations_threshold:friendly
					}
					value = 50
				}
			}

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_INFAMY"
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:infamous
						"scope:actor.infamy" < infamy_threshold:notorious
					}
					value = -10
				}
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:notorious
						"scope:actor.infamy" < infamy_threshold:pariah
					}
					value = -25
				}
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:pariah
					}
					value = -50
				}
			}

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_RANK"

				if = {
					limit = { scope:actor = { country_rank = root.country_rank } }
					value = -20
				}

				if = {
					limit = { scope:actor = { country_rank < root.country_rank } }
					value = -50
				}
			}
		}
		
		will_break = { 
			is_junior_in_customs_union = yes		
			OR = {
				has_attitude = {
					who = scope:target_country
					attitude = antagonistic
				}
				has_attitude = {
					who = scope:target_country
					attitude = domineering
				}
				has_attitude = {
					who = scope:target_country
					attitude = belligerent
				}
				has_attitude = {
					who = scope:target_country
					attitude = rebellious
				}			
			}
		}

		propose_score = {
			value = 10
		}

		propose_break_score = {
			value = 10
		}

		use_obligation_chance = {
			value = 25
		}

		owe_obligation_chance = {
			value = 0
		}
	}
}

join_customs_union = {
	requires_approval = yes
	can_use_obligations = yes

	potential = {
		NOT = { is_country_type = decentralized }

		scope:target_country = {
			NOT = { is_country_type = decentralized }
		}
	}

	possible = {
		NOT = { has_war_with = scope:target_country }

		# if we are the senior partner in an existing customs union,
		# we don't want to join someone else's union
		# if we are the junior partner in an existing customs union,
		# we cannot join another union anyways
		is_in_customs_union = no
		
		has_diplomatic_relevance = scope:target_country

		NOT = {
			has_law = law_type:law_isolationism					
		}

		custom_tooltip = {
			text = DIPLO_ACTION_RELATIONS_CORDIAL_OR_GREATER
			scope:target_country.relations:root >= relations_threshold:cordial
		}

		is_revolutionary = no

		scope:target_country = {
			is_revolutionary = no
		}
		
		custom_tooltip = {
			text = DIPLO_ACTION_MARKET_CONNECTION_REQUIRED
			OR = {	
				AND = {
					market = { has_port = yes }
					scope:target_country.market = { has_port = yes }
				}
				market = {
					any_scope_country = {
						is_adjacent = scope:target_country
					}
				}
			}
		}		
	}

	reverse_pact = customs_union

	accept_effect = {
		if = {
			limit = {
				has_diplomatic_pact = {
					who = scope:target_country
					type = trade_agreement
				}			
			}
			remove_diplomatic_pact = {
				country = scope:target_country
				type = trade_agreement
			}			
		}		
	}

	ai = {
		check_acceptance_for_will_propose = yes

		propose_score = {
			value = 10
		}

		propose_break_score = {
			value = 10
		}

		use_obligation_chance = {
			value = 10
		}

		owe_obligation_chance = {
			value = 10
		}
	}
}

change_customs_union = {
	requires_approval = yes
	can_use_obligations = yes

	potential = {
		NOT = { is_country_type = decentralized }
		is_junior_in_customs_union = no
		scope:target_country = {
			NOT = { is_country_type = decentralized }
			 is_junior_in_customs_union = yes
		}
	}

	possible = {
		NOT = { has_war_with = scope:target_country }
		
		has_diplomatic_relevance = scope:target_country

		NOR = {
			scope:target_country = { has_law = law_type:law_isolationism }
			has_law = law_type:law_isolationism					
		}

		custom_tooltip = {
			text = DIPLO_ACTION_RELATIONS_CORDIAL_OR_GREATER
			scope:target_country.relations:root >= relations_threshold:cordial
		}

		is_revolutionary = no

		scope:target_country = {
			is_revolutionary = no
		}
		
		custom_tooltip = {
			text = DIPLO_ACTION_MARKET_CONNECTION_REQUIRED_CHANGE
			OR = {	
				AND = {
					market = { has_port = yes }
					scope:target_country = { has_port = yes }
				}
				market = {
					any_scope_country = {
						is_adjacent = scope:target_country
					}
				}
			}
		}		
	}

	transfer_pact = customs_union

	accept_effect = {
		if = {
			limit = {
				has_diplomatic_pact = {
					who = scope:target_country
					type = trade_agreement
				}			
			}
			remove_diplomatic_pact = {
				country = scope:target_country
				type = trade_agreement
			}			
		}		
	}

	ai = {
		check_acceptance_for_will_propose = yes

		propose_score = {
			value = 10
		}

		propose_break_score = {
			value = 10
		}

		use_obligation_chance = {
			value = 50
		}

		owe_obligation_chance = {
			value = 10
		}
	}
}