economic_union = {
	requires_approval = yes
	should_notify_third_parties = yes
	can_select = no
	can_use_obligations = yes

	unlocking_technologies = {
		international_trade
	}

	potential = {
		always = no
	}

	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_economic_union # A customs union is technically a type of subject, though it isn't presented this way in-game
		
		actor_can_break = {
			is_diplomatic_play_committed_participant = no
		}

		target_can_break = {
			is_diplomatic_play_committed_participant = no
		}

		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 )
		
		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_break_score = {
			value = 10
		}
		
		accept_break_score = {
			value = 0

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_BASE"
				value = -100
			}				
	
			if = {
				limit = { has_truce_with = scope:actor }
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_TRUCE"
					value = -1000
				}	
			}			
			
			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_ARMY_STRENGTH"
				value = "scope:actor.ai_army_comparison(root)"
				add = 0.5
				multiply = 5
				min = -50
				max = 50
			}		

			if = {
				limit = {
					"scope:actor.ai_ideological_opinion(root)" > 0
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_IDEOLOGICAL_OPINION"
					value = "scope:actor.ai_ideological_opinion(root)"
					multiply = -0.25
					min = -10
				}					
			}
			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.25
					max = 10
				}					
			}							
			
			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_ATTITUDE"	
				if = {
					limit = {
						has_attitude = {
							who = scope:actor
							attitude = domineering
						}								
					}
					value = -50
				}							
			}			

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_RELATIONS"	
				if = {
					limit = {
						"relations(scope:actor)" < relations_threshold:cordial
					}
					value = -25
				}					
				if = {
					limit = {
						"relations(scope:actor)" >= relations_threshold:cordial
						"relations(scope:actor)" < relations_threshold:amicable
					}
					value = 10
				}					
				if = {
					limit = {
						"relations(scope:actor)" >= relations_threshold:amicable
						"relations(scope:actor)" < relations_threshold:friendly
					}
					value = 25
				}	
				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 = -25
				}	
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:notorious
						"scope:actor.infamy" < infamy_threshold:pariah
					}
					value = -50
				}
				if = {
					limit = {
						"scope:actor.infamy" >= infamy_threshold:pariah
					}
					value = -100
				}					
			}					

			add = {
				desc = "DIPLOMATIC_ACCEPTANCE_RANK"
	
				if = {
					limit = { scope:actor = { country_rank > root.country_rank } }
					value = 10
				}
				
				if = {
					limit = { scope:actor = { country_rank < root.country_rank } }
					value = -10
				}				
			}	

			if = {
				limit = {
					any_country = {
						NOT = { this = root }
						OR = {
							country_rank >= root.country_rank
							army_size >= root.army_size
						}
						
						OR = {
							has_diplomatic_pact = {
								who = scope:actor
								type = alliance
							}
							has_diplomatic_pact = {
								who = scope:actor
								type = customs_union
							}
							has_diplomatic_pact = {
								who = scope:actor
								type = defensive_pact
							}									
						}							
					}
				}
				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_POWERFUL_PROTECTORS_US"
					value = 25
				}	
			}		
		}

		use_obligation_chance = {
			value = 50
		}

		owe_obligation_chance = {
			value = 25
		}
	}
}