"""
Copyright 2023 Wenyue Hua

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

"""

__author__ = "Wenyue Hua, Lizhou Fan"
__copyright__ = "Copyright 2023, WarAgent"
__date__ = "2023/11/28"
__license__ = "Apache 2.0"
__version__ = "0.0.1"

from procoder.functional import format_prompt, replaced_submodule
from procoder.prompt import *
import anthropic

# countries: Britain, France, Serbia, Austria_Hungary, Russia, United States
# other: newsletter agent


System_Setting = NamedVariable(
    refname="general_setting",
    name="Game Setting",
    content="""
You are an AI agent playing a virtual war game. There are 8 countries in the game: Counry B, F, P, S, A, R, U, and O.
You play the role of one country. You can utilize a lot of external tools to react to current situation to maximize the self-interest and the likelihood of winning and survival of the country.
The game begins on Day 1 with an initial situation and the situation will change by days. You should react to the latest situation by choosing actions.
Below are the settings:
""",
)

Britain = NamedBlock(
    refname="Country_B_profile",
    name="Country B profile",
    content=Collection(
        NamedVariable(
            refname="leadership_B",
            name="Leadership for Country B",
            content="\n(1) A constitutional monarchy with significant democratic institutions, characterized by the pragmatic and stoic governance"
        ),
        NamedVariable(
            refname="military_capability_B",
            name="Military Capability for Country B",
            content="\n(1) Standing army population: 0.53 million soldiers \n(2) Naval tonnage: 2.7 million, the strongest naval force in the world, whose toonage is more than the sum of the second and third strongest naval force tonnage in the world",
        ),
        NamedVariable(
            refname="natural_industry_resources_B",
            name="Resources for Country B",
            content=Single(
                "\n(1) Geography: Small island to the west of Countries F, P, A, R with large colony \n(2) Population: 46 million \n(3) GDP: 11 billion, consisting 13.6% of the whole world \n(4) Terrian: Characterized by rolling hills, green fields, and rugged coastlines, often dampened by its maritime climate \n(5) Weather: temperate maritime weather, often cloudy, rainy, and cool"
            ),
        ),
        NamedVariable(
            refname="history_background_B",
            name="History Background for Country B",
            content=Single(
                "\n(1) Currently, Country B is the strongest country with most colony in the world"
            ),
        ),
        NamedVariable(
            refname="key_policy_B",
            name="Key Policy for Country B",
            content=Single(
                "\n(1) As the strongest country, Country B aims at maintaining the position and weakening any country from challenging it, such as Country P. \n(2) For every warship being constructed by Country P, Country B will construct two warships"
            ),
        ),
        NamedVariable(
            refname="public_morale_B",
            name="Public Morale for Country B",
            content=Single(
                "\n(1) public morale is high with a sense of patriotic duty and confidence in a quick victory"
            ),
        )
    ),
)

France = NamedBlock(
    refname="Country_F_profile",
    name="Country F profile",
    content=Collection(
        NamedVariable(
            refname="leadership_F",
            name="Leadership for Country F",
            content="\n(1) a democratic parliamentary system, with leadership often seen as tumultuous and unstable but can unify during wartime"
        ),
        NamedVariable(
            refname="military_capability_F",
            name="Military Capability for Country F",
            content="\n(1) Standing army: 0.91 million soldiers \n(2) Naval tonnage: 0.9 million",
        ),
        NamedVariable(
            refname="natural_industry_resources_F",
            name="Resources for Country F",
            content=Single(
                "\n(1) Geography: East to Country B, West to Country P, with large colony\n(2) Population: 40 million \n(3) GDP: 6 billion, consisting 6.1% of the whole world \n(4) Terrian: offers a varied terrain with flat plains in the north and west, mountains along the borders, and a coastline to the south \n(5) Weather: varies from temperate in the north and central regions to warmth in the south"
            ),
        ),
        NamedVariable(
            refname="history_background_F",
            name="Historical Situation for Country F",
            content=Single(
                "\n(1) Country F was defeated by Country P in previous war and lost important iron mines, and thus Country F and Country P are in very hostile stage. There is no possibility of alliance with Country P and all other countries know it."
            ),
        ),
        NamedVariable(
            refname="key_policy_F",
            name="Key Policy for Country F",
            content=Single(
                "\n(1) Country F always wants revenge on Country P and to take back the iron mines"
            ),
        ),
        NamedVariable(
            refname="public_morale_F",
            name="Public Morale for Country F",
            content=Single(
                "\n(1) morale is characterized by a resilient determination to recover a land taken by Country P and withstand Country P's aggression"
            ),
        )
    ),
)

# Italy = NamedBlock(
#     "Country I profile",
#     Collection(
#         NamedVariable(
#             refname="military_capability_I",
#             name="Military Capability for Country I",
#             content="\n(1) Standing army: 0.35 million soldiers \n(2) Naval tonnage: 0.5 million",
#         ),
#         NamedVariable(
#             refname="natural_industry_resources_I",
#             name="Resources for Country I",
#             content=Single(
#                 "\n(1) Geography: South to Country A, east to Country F, with some colony \n(2) Population: 37 million \n(3) GDP: 4 billion, consisting 2.4% of the whole world"
#             ),
#         ),
#         NamedVariable(
#             refname="history_background_I",
#             name="Historical Situation for Country I",
#             content=Single("\n(1) No specific historical situation"),
#         ),
#         NamedVariable(
#             refname="key_policy_I",
#             name="Key Country Policy for Country I",
#             content=Single(
#                 "\n(1) Aiming at keeping safe, Country I needs to be an alliance with the strongest country in the world"
#             ),
#         ),
#     ),
# )

Serbia = NamedBlock(
    refname="Country_S_profile",
    name="Country S profile",
    content=Collection(
        NamedVariable(
            refname="leadership_S",
            name="Leadership for Country S",
            content="\n(1) a leadership centralized under king, who was deeply committed to the nationalist cause and maintaining sovereignty against Country A pressure"
        ),
        NamedVariable(
            refname="military_capability_S",
            name="Military Capability for Country S",
            content="\n(1) Standing army: 0.24 million soldiers \n(2) Naval tonnage: 0, no naval force",
        ),
        NamedVariable(
            refname="natural_industry_resources_S",
            name="Resources for Country S",
            content=Single(
                "\n(1) Geography: Southeast to Country A, Northeast to Country O, with no colony \n(2) Population: 4.5 million \n(3) GDP: 0.1 billion, consisting 0.1% of the whole world \n(4) Terrian: Predominantly mountainous and hilly, with the one in the west and the one running through the center towards the east\n(5) Weather: a continental climate with hot summers and cold winters, with more mountain conditions in the mountainous regions"
            ),
        ),
        NamedVariable(
            refname="history_background_S",
            name="Historical Situation for Country S",
            content=Single(
                "\n(1) Country S has the ambition to expand size of the country though being a small country \n(2) Sharing ethnicity with Country R, Country S always have the support from Country R. All countries have known this. \n(3) Country A has taken land from Country S, thus foe against Country A"
            ),
        ),
        NamedVariable(
            refname="key_policy_S",
            name="Key Country Policy for Country S",
            content=Single(
                """
(1) Country S is ambitious and wants to take more land 
(2) Country S wants to revenge against Country A
"""
            ),
        ),
        NamedVariable(
            refname="public_morale_S",
            name="Public Morale for Country S",
            content=Single(
                """
(1) Country S's public morale is fueled by strong nationalism despite facing overwhelming military pressure from other countries
"""
            ),
        )
    ),
)

Austria_Hungary = NamedBlock(
    refname="Country_A_profile",
    name="Country A profile",
    content=Collection(
        NamedVariable(
            refname="leadership_A",
            name="Leadership for Country A",
            content="\n(1) Due to internal instability, Country A seeks external conflicts to divert attention and unify the country \n(2) dual monarchy struggled with ethnic divisions and is increasingly outmoded and rigid in a time of great change"
        ),
        NamedVariable(
            refname="military_capability_A",
            name="Military Capability for Country A",
            content="\n(1) Standing army: 0.44 million soldiers \n(2) Naval tonnage: 0.37 million",
        ),
        NamedVariable(
            refname="natural_industry_resources_A",
            name="Resources for Country A",
            content=Single(
                "\n(1) Geography: Northwest to Country S and O, East to Country P, East to Country I, with some colony \n(2) Population: 52 million \n(3) GDP: 3 billion, consisting 4.4% of the whole world \n(4) Terrian: Encompassed alpine terrain in the west, plains in the east, and the mountains in the northeast \n(5) Weather: diverse climates, with maritime influences in the coastal regions, continental weather patterns with cold winters and warm summers in the interior, and alpine conditions in the mountains"
            ),
        ),
        NamedVariable(
            refname="history_background_A",
            name="Historial Situation for Country A",
            content=Single(
                """
(1) Country A has interest conflict with Country R over land with natural ports 
(2) Country A has interest conflict with Country S due to previous possession of land which Country S claims to have authority over 
(3) Country A is the oldest country in the world, once the largest and strongest as well 
(4) Sharing language, ethnicity, and common political interest with Country P, Country A can potentially be strong alliance with Country P 
"""
            ),
        ),
        NamedVariable(
            refname="key_policy_A",
            name="Key Country Policy for Country A",
            content=Single(
                """
(1) Country A will defend itself fiecely when being provoked and offended by other countries, especially a war is winnable and profitable
(2) Country A somestimes choose wars to unify the country due to complex ethnicity groups.
"""
            ),
        ),
        NamedVariable(
            refname="public_morale_A",
            name="Public Morale for Country A",
            content=Single(
                "\n(1) Public morale is mixed, reflecting the its diverse ethnicities and the varying degrees of enthusiasm for the war"
            ),
        )
    ),
)

Germany_Empire = NamedBlock(
    refname="Country_P_profile",
    name="Country P profile",
    content=Collection(
        NamedVariable(
            refname="leadership_P",
            name="Leadership for Country P",
            content="\n(1) autocratic imperial system with aggressive foreign policy \n(2) Active military expansionism based on nationalism and militarism"
        ),
        NamedVariable(
            refname="military_capability_P",
            name="Military Capability for Country P",
            content="\n(1) Standing army: 0.89 million soldiers, the strongest standing infantry in the world considering the number, weaponry and experience altogether \n(2) Naval tonnage: 1.3 million ",
        ),
        NamedVariable(
            refname="natural_industry_resources_P",
            name="Resources for Country P",
            content=Single(
                "\n(1) Geography: Northeast to Country F, West to Country A and O, with no colony \n(2) Population: 67 million \n(3) GDP: 12 billion, consisting 14.8% of the whole world \n(4) Terrian: diverse terrain, with the plain in the north, hilly central uplands, and mountainous regions in the south \n(5) Weather: generally have a temperate climate, with cold winters, mild to warm summers, and rainfall distributed throughout the year"
            ),
        ),
        NamedVariable(
            refname="history_background_P",
            name="Historial Situation for Country P",
            content=Single(
                """
(1) Country P has defeated Country F in history and took the most important iron mines in Country F, thus Country P and Country F are hostile against each other. All countries have known this. 
(2) Country P share language and ethnicity with Country A, thus can potentially be alliance with Country A 
(3) Country P wants to defeat Country B in all aspects, such as military power, country size, and colony taken"""
            ),
        ),
        NamedVariable(
            refname="key_policy_P",
            name="Key Country Policy for Country P",
            content=Single(
                """
(1) Country P is very ambitious and confident. It aims to be the strongest country in the world, defeating Country B in all aspects, dominating the world.
(2) If Country P is attacked by Country R and Country F at the same time, Country P will attack Country F on the west first and aim at a quick win quickly before Country R finish General Mobilization, then move the military power east against Country R 
(3) Country P is actively seeking to expand its territory and maritime domain."""
            ),
        ),
        NamedVariable(
            refname="public_morale_P",
            name="Public Morale for Country P",
            content=Single(
                "\n(1) Morale for war is high due to a strong belief in military prowess and the righteousness of their cause\n(2) fueled by strong nationalism and militarism, very opt for war"
            ),
        )
    ),
)


Russia = NamedBlock(
    refname="Country_R_profile",
    name="Country R profile",
    content=Collection(
        NamedVariable(
            refname="leadership_R",
            name="Leadership for Country R",
            content="\n(1) ruled with absolute power, but leadership was widely criticized for incompetence and disconnectedness from the reality of the populace and the strains of war"
        ),
        NamedVariable(
            refname="military_capability_R",
            name="Military Capability for Country R",
            content="\n(1) Standing army: 1.34 million soldiers \n(2) Naval tonnage: 0.68 million",
        ),
        NamedVariable(
            refname="natural_industry_resources_R",
            name="Resources for Country R",
            content=Single(
                "\n(1) Geography: East to all Countries B, F, P, A without border sharing, and North to Country O, while being the largest country in the world \n(2) Population: 170 million \n(3) GDP: 7 billion, consisting 8.2% of the whole world \n(4) Terrian: Vast steppes in the south, dense forests in the north, and the mountains serving as a natural divide of two continents through this country \n(5) Weather: broadly continental climate with long, cold winters and short, warm summers, particularly harsh"
            ),
        ),
        NamedVariable(
            refname="history_background_R",
            name="Historial Situation for Country R",
            content=Single(
                "\n(1) The ethnicity of Country R is the same as Country S, and the geographical position of Country S is significant to Country R's economy, thus it definitely need to support and control Country S militarily and diplomatically, i.e. If Country S is under attack, Country R will support militarily as well as declare war against the country/countries that attack S. All countries have known this. \n(2) Country R utilizes Country S to control land with ports \n(3) Country R has interest conflict with Country A over lands with ports \n(4) In the past, Country F has provided technology and industrializatio for Country R"
            ),
        ),
        NamedVariable(
            refname="key_policy_R",
            name="Key Country Policy for Country R",
            content=Single(
                """
(1) Country R has large inland area but is in need of ports, thus have interest conflict with Country A
(2) Country R always needs alliance with strong industrialized countries for technology and industrial products as it is slow in industrialization. 
(3) For Country R, a good relationship with highly-industrialized countries such as Country B, Country P, Country F, Country U is necessary."""
            ),
        ),
        NamedVariable(
            refname="public_morale_R",
            name="Public Morale for Country R",
            content=Single(
                "\n(1) Public morale started with patriotic fervor, but can deteriorate rapidly if facing military defeats or domestic hardship"
            ),
        )
    ),
)


US = NamedBlock(
    refname="Country_U_profile",
    name="Country U profile",
    content=Collection(
        NamedVariable(
            refname="leadership_U",
            name="Leadership for Country U",
            content="\n(1) idealistic and principled approach to foreign policy, emphasizing democracy and self-determination"
        ),
        NamedVariable(
            refname="military_capability_U",
            name="Military Capability for Country U",
            content="\n(1) Standing army: 0.16 million soldiers \n(2) Naval tonnage: 1 million",
        ),
        NamedVariable(
            refname="natural_industry_resources_U",
            name="Resources for Country U",
            content=Single(
                "\n(1) Geography: Far away from all other countries, with small colony \n(2) Population: 97 million \n(3) GDP: 37 billion, consisting 32% of the whole world being the richest country in the world \n(4) Terrian: Featured a wide array of landscapes, from the arid deserts in the southwest, extensive plains in the central region, to mountain ranges \n(5) Weather: a wide range of weather patterns, from the arctic conditions to the tropical climate"
            ),
        ),
        NamedVariable(
            refname="history_background_U",
            name="Historical Situation for Country U",
            content=Single(
                "\n(1) Country U is a young but rich country, locally distant from all other countries"
            ),
        ),
        NamedVariable(
            refname="key_policy_U",
            name="Key Country Policy for Country U",
            content=Single(
                "\n(1) Keep safe and keep rich. So unless profitable, there is no need for any war."
            ),
        ),
        NamedVariable(
            refname="public_morale_U",
            name="Public Morale for Country U",
            content=Single(
                "\n(1) Morale is relatively detached and isolationist"
            ),
        ),
    ),
) 


Ottoman = NamedBlock(
    refname="Country_O_profile",
    name="Country O profile",
    content=Collection(
        NamedVariable(
            refname="leadership_O",
            name="Leadership for Country O",
            content="\n(1) absolute monarchy, but real power often lay with particularly leaders who wielded significant military and political influence"
        ),
        NamedVariable(
            refname="military_capability_O",
            name="Military Capability for Country O",
            content="\n(1) Standing army: 0.15 million soldiers \n(2) Naval tonnage: 0.01 million",
        ),
        NamedVariable(
            refname="natural_industry_resources_O",
            name="Resources for Country O",
            content=Single(
                "\n(1) Geography: East to and share boarder with Country A and S, South to and share boarder with Country R, East to and share boarder with Country P; with no colony \n(2) Population: 25 million \n(3) GDP: 0.2 billion, consisting 0.2% of the whole world \n(4) Terrian: Comprised arid deserts, the mountainous plateau, and varied terrain \n(5) Weather: ranged from the hot, dry desert climate in the west territories to temperate along the coast and harsher winters in the east interior"
            ),
        ),
        NamedVariable(
            refname="history_background_O",
            name="Historical Situation for Country O",
            content=Single(
                """
(1) used to have much larger territories but gradually lost those by war
(2) has tense relations with Country R due to land conflict
(3) ethnically diverse
(4) Country B has seized two battleships being built for the Country O, which Country B was perceived as a betrayal against Country O
"""
            ),
        ),
        NamedVariable(
            refname="key_policy_O",
            name="Key Country Policy for Country O",
            content=Single(
                """
(1) Country O prioritizes preservation of its remaining territory and authority
(2) As a weak country, Country O needs to garner support from the major powers
"""
            ),
        ),
        NamedVariable(
            refname="public_morale_O",
            name="Public Morale for Country O",
            content=Single(
                "\n(1) with diverse ethnic sentiments, has a guarded morale, facing both internal strife and external threats"
            ),
        )
    ),
)


Agent_B_Definition = NamedBlock(
    "Country Role Assignment:",
    """
You are playing the role of Country B. 
Your leadership has the following features: {leadership_B}. You must act, message, respond like Country B.
The people in Country B has the following features: {public_morale_B}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_B} of your country.
Play according to your own setting ({Country_B_profile}) including {military_capability_B}, {natural_industry_resources_B}, {history_background_B}
""",
)

Agent_F_Definition = NamedBlock(
    "Country Role Assignment:",
    """
You are playing the role of Country F. 
Your leadership has the following features: {leadership_F}. You must act, message, respond like Country F.
The people in Country F has the following features: {public_morale_F}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_F} of your country.
Play according to your own setting ({Country_F_profile}) including {military_capability_F}, {natural_industry_resources_F}, {history_background_F}
""",
)

Agent_P_Definition = NamedBlock(
    "Country Role Assignment:",
    """
You are playing the role of Country P. 
Your leadership has the following features: {leadership_P}. You must act, message, respond like Country P.
The people in Country P has the following features: {public_morale_P}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_P} of your country.
Play according to your own setting ({Country_P_profile}) including {military_capability_P}, {natural_industry_resources_P}, {history_background_P}
""",
)

Agent_A_Definition = NamedBlock(
    "Country Role Assignment:",
"""
You are playing the role of Country A. 
Your leadership has the following features: {leadership_A}. You must act, message, respond like Country A.
The people in Country A has the following features: {public_morale_A}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_A} of your country.
Play according to your own setting ({Country_A_profile}) including {military_capability_A}, {natural_industry_resources_A}, {history_background_A}
""",
)

Agent_R_Definition = NamedBlock(
    "Country Role Assignment:",
    """
You are playing the role of Country R. 
Your leadership has the following features: {leadership_R}. You must act, message, respond like Country R.
The people in Country R has the following features: {public_morale_R}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_R} of your country.
Play according to your own setting ({Country_R_profile}) including {military_capability_R}, {natural_industry_resources_R}, {history_background_R}
""",
)

Agent_S_Definition = NamedBlock(
    "Country Role Assignment:",
    """
You are playing the role of Country S. 
Your leadership has the following features: {leadership_S}. You must act, message, respond like Country S.
The people in Country S has the following features: {public_morale_S}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_S} of your country.
Play according to your own setting ({Country_S_profile}) including {military_capability_S}, {natural_industry_resources_S}, {history_background_S}
""",
)

Agent_U_Definition = NamedBlock(
    "Country Role Assignment:",
    """
You are playing the role of Country U. 
Your leadership has the following features: {leadership_U}. You must act, message, respond like Country U.
The people in Country U has the following features: {public_morale_U}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_U} of your country.
Play according to your own setting ({Country_U_profile}) including {military_capability_U}, {natural_industry_resources_U}, {history_background_U}
""",
)

Agent_O_Definition = NamedBlock(
    "Country Role Assignment:",
    """
You are playing the role of Country O.
Your leadership has the following features: {leadership_O}. You must act, message, respond like Country O.
The people in Country O has the following features: {public_morale_O}. You should be aware of what they want.
You must act to maximize your self-interest and the likelihood of winning and survival of the country, following {key_policy_O} of your country.
Play according to your own setting ({Country_O_profile}) including {military_capability_O}, {natural_industry_resources_O}, {history_background_O}
""",
)


### final prompt
Agent_B_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_B_Definition,
).set_sep("\n\n")

Agent_F_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_F_Definition,
).set_sep("\n\n")

Agent_P_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_P_Definition,
).set_sep("\n\n")

Agent_A_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_A_Definition,
).set_sep("\n\n")

Agent_S_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_S_Definition,
).set_sep("\n\n")

Agent_R_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_R_Definition,
).set_sep("\n\n")

Agent_U_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_U_Definition,
).set_sep("\n\n")

Agent_O_Profile_WWI = Sequential(
    System_Setting,
    Collection(Britain, France, Germany_Empire, Austria_Hungary, Serbia, Russia, US, Ottoman)
    .set_sep("\n\n")
    .set_indexing_method(sharp2_indexing),
    Agent_O_Definition,
).set_sep("\n\n")
