﻿INJECT:foreign_investment_rights = {
	ai = {

		treaty_categories = {
			economy
		}

		evaluation_chance = {
			value = 0.1

			add = {
				value = country_rank
				multiply = 0.05
			}
		}

		article_ai_usage = {
            request
        }

		inherent_accept_score = {
			value = 0

			if = {
				limit = {
					scope:source_country ?= this
				}

				# We are doing this to be able to reuse loc across treaties and diplomatic actions
				scope:source_country = {
					save_temporary_scope_as = target
				}
				scope:target_country = {
					save_temporary_scope_as = actor
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_BASE_ADDITIONAL"
					value = -10
				}

				if = {
					limit = {
						has_law_or_variant = law_type:law_isolationism
					}
					add = {
						desc = "law_isolationism"
						add = -300
					}
				}

				if = {
					limit = {
						any_diplomatic_catalyst = {
							is_diplomatic_catalyst_type = catalyst_declared_bankruptcy
							target ?= scope:target_country
						}
					}
					add = {
						desc = "catalyst_declared_bankruptcy"
						add = 25
					}
				}

				if = {
					limit = {
						gdp >= 2000000
					}

					subtract = {
						desc = "DIPLOMATIC_ACCEPTANCE_HIGH_GDP"
						value = gdp
						subtract = 1000000
						divide = 1000000
						min = 1
						max = 50
					}
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_GDP"
					value = "scope:target_country.ai_gdp_comparison(scope:source_country)"
					multiply = 10
					min = -50
					max = 10
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_MUTUAL_TRADE_VALUE"
					value = "mutual_trade_value_with_country(scope:target_country)"
					multiply = 50000
					divide = { # We modulate this value by GDP, clamped to a range to ensure it remains relevant for very small/very large economies
						value = gdp
						max = scope:target_country.gdp # We use whichever GDP is smaller
						min = 1000000
					}
					max = 10
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_ATTITUDE"
					if = {
						limit = {
							OR = {
								has_attitude = {
									who = scope:target_country
									attitude = antagonistic
								}
								has_attitude = {
									who = scope:target_country
									attitude = belligerent
								}
								has_attitude = {
									who = scope:target_country
									attitude = rebellious
								}
							}
						}
						value = -1000
					}
				}

				if = {
					limit = {
						c:JAP ?= scope:source_country
						scope:source_country ?= {
							has_journal_entry = je_meiji_imperial_marriage
						}
					}
					add = {
						desc = "je_meiji_imperial_marriage"
						value = -500
					}
				}
			}
			else = {
				scope:source_country = {
					save_temporary_scope_as = actor
				}
				scope:target_country = {
					save_temporary_scope_as = target
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_BASE_ADDITIONAL"
					value = -10
				}

				if = {
					limit = {
						has_law_or_variant = law_type:law_isolationism
					}
					add = {
						desc = "law_isolationism"
						add = -300
					}
				}

				if = {
					limit = {
						scope:target_country ?= {
							modifier:country_construction_add <= 50
						}
					}
					add = {
						desc = "DIPLOMATIC_ACCEPTANCE_LOW_CONSTRUCTION"
						value = scope:target_country.modifier:country_construction_add
						subtract = 50
					}
				}

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

				if = {
					limit = {
						scope:source_country = {
							any_diplomatic_catalyst = {
								is_diplomatic_catalyst_type = catalyst_declared_bankruptcy
								target ?= scope:target_country
							}
						}
					}
					add = {
						desc = "catalyst_declared_bankruptcy"
						add = 25
					}
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_MUTUAL_TRADE_VALUE"
					value = "mutual_trade_value_with_country(scope:source_country)"
					multiply = 50000
					divide = { # We modulate this value by GDP, clamped to a range to ensure it remains relevant for very small/very large economies
						value = gdp
						max = scope:source_country.gdp # We use whichever GDP is smaller
						min = 1000000
					}
					max = 25
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_GDP"
					value = "scope:source_country.ai_gdp_comparison(scope:target_country)"
					add = 10.0 # Bonus if they have at least 10% of the target's GDP
					multiply = 5
					min = 0
					max = 25
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_ATTITUDE"
					if = {
						limit = {
							OR = {
								has_attitude = {
									who = scope:source_country
									attitude = antagonistic
								}
								has_attitude = {
									who = scope:source_country
									attitude = domineering
								}
								has_attitude = {
									who = scope:source_country
									attitude = belligerent
								}
								has_attitude = {
									who = scope:source_country
									attitude = rebellious
								}
							}
						}
						value = -1000
					}
				}

				add = {
					desc = "DIPLOMATIC_ACCEPTANCE_INCOMPATIBLE_ECONOMIC_SYSTEMS"
					if = {
						limit = {
							OR = {
								AND = {
									scope:source_country = { has_radical_leftist_economy = no }
									scope:target_country = { has_radical_leftist_economy = yes }
								}
								AND = {
									scope:source_country = { has_radical_leftist_economy = yes }
									scope:target_country = { has_radical_leftist_economy = no }
								}
							}
						}
						value = -100
					}
				}

				add = {
					desc = "je_hawaiian_interest"
					if = {
						limit = {
							has_journal_entry = je_hawaiian_interest
							c:HAW ?= {
								this = scope:source_country
							}
						}
						value = 20
					}
				}
			}
		}

		contextual_accept_score = {
			if = {
				limit = {
					scope:treaty ?= {
						NOT = {
							any_scope_article_option = {
								NOT = { has_type = foreign_investment_rights }
								NOT = { is_desired_by = root }
							}
						}
						any_scope_article_option = {
							has_type = foreign_investment_rights
						}
					}
				}
				if = {
					limit = {
						scope:target_country = {
							has_variable = pro_lobby_proposal_investment_agreement
							var:pro_lobby_opportunity_target ?= scope:source_country
						}
					}
					add = {
						desc = "LOBBY_OPPORTUNITY_BONUS"
						value = 50
					}
				}

				if = {
					limit = {
						scope:target_country = {
							has_variable = anti_lobby_proposal_investment_agreement
							var:anti_lobby_opportunity_target ?= scope:source_country
						}
					}
					add = {
						desc = "LOBBY_OPPORTUNITY_BONUS"
						value = 25
					}
				}
			}
			else = {
				if = {
					limit = {
						scope:target_country = {
							has_variable = pro_lobby_proposal_investment_agreement
							var:pro_lobby_opportunity_target ?= scope:source_country
						}
					}
					add = {
						desc = "LOBBY_OPPORTUNITY_BONUS"
						value = 10
					}
				}

				if = {
					limit = {
						scope:target_country = {
							has_variable = anti_lobby_proposal_investment_agreement
							var:anti_lobby_opportunity_target ?= scope:source_country
						}
					}
					add = {
						desc = "LOBBY_OPPORTUNITY_BONUS"
						value = 5
					}
				}
			}
		}

		# KAI: Replaced `target_country`, which is actually the attacker, with `source_country`.
		# Added a check against subjects, since the treaty will be broken immediately if a country does not have diplomatic autonomy.
		#
		# Since hostile attitude gives -1000 treaty acceptance score, it is inherited in this wargoal.
		# As a result the break embargo part doesn't really work.
		wargoal_score_multiplier = {
			value = 1

			if = {
				limit = {
					scope:source_country = {
						has_diplomatic_pact = {
							who = root
							type = embargo
						}
					}
					# can_reach_target_country = yes
				}
				add = 1
			}

			# Not worth doing by or against very small countries
			if = {
				limit = {
					OR = {
						gdp < 2000000
						scope:source_country = {
							OR = {
								gdp < 500000
								# KAI: Don't demand invesment rights from subjects
								# The treaty will break if the country does not have diplomatic autonomy
								# Plus it's better to just demand it from the overlord
								is_subject = yes
							}
						}
					}
				}
				multiply = 0
			}
		}
	}
}
