#!/bin/bash

# Export SMOM Country Relationships to CSV
# Generates CSV files for Sovereign Order of Hospitallers country relationships

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
OUTPUT_DIR="${PROJECT_ROOT}/docs/infrastructure/data"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

mkdir -p "${OUTPUT_DIR}"

# Create comprehensive CSV with all countries
cat > "${OUTPUT_DIR}/smom_countries_full_${TIMESTAMP}.csv" << 'EOF'
Country,Region,Relationship Type,Priority,Cloudflare Coverage,Network Infrastructure Priority,Notes
Angola,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Benin,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Burundi,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Burkina Faso,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Cameroon,Africa (Sub-Saharan),Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Cape Verde,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Central African Republic,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Chad,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Comoros,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Democratic Republic of the Congo,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Republic of the Congo,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Côte d'Ivoire,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Equatorial Guinea,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Eritrea,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Ethiopia,Africa (Sub-Saharan),Full Diplomatic Relations,High,Yes,Regional Datacenter,
Gabon,Africa (Sub-Saharan),Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
The Gambia,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Guinea,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Guinea-Bissau,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Kenya,Africa (Sub-Saharan),Full Diplomatic Relations,High,Yes,Regional Datacenter,
Lesotho,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Liberia,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Madagascar,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Mali,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Mauritania,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Mauritius,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Mozambique,Africa (Sub-Saharan),Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Namibia,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Niger,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
São Tomé and Príncipe,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Senegal,Africa (Sub-Saharan),Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Seychelles,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Sierra Leone,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Somalia,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
South Sudan,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Sudan,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Togo,Africa (Sub-Saharan),Full Diplomatic Relations,Low,Yes,Edge/CDN,
Egypt,Middle East & North Africa,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Jordan,Middle East & North Africa,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Lebanon,Middle East & North Africa,Full Diplomatic Relations,High,Yes,Regional Datacenter,
Morocco,Middle East & North Africa,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Antigua and Barbuda,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Argentina,Americas,Full Diplomatic Relations,High,Yes,Regional Datacenter,
The Bahamas,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Belize,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Bolivia,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Brazil,Americas,Full Diplomatic Relations,High,Yes,Core Datacenter,
Chile,Americas,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Colombia,Americas,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Costa Rica,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Cuba,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Dominican Republic,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Ecuador,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
El Salvador,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Grenada,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Guatemala,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Guyana,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Haiti,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Honduras,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Nicaragua,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Panama,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Paraguay,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Peru,Americas,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Saint Lucia,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Saint Vincent and the Grenadines,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Suriname,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Uruguay,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Venezuela,Americas,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Afghanistan,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Armenia,Asia-Pacific,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Cambodia,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Georgia,Asia-Pacific,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Kazakhstan,Asia-Pacific,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Kiribati,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Marshall Islands,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Micronesia (Federated States of),Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Nauru,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Philippines,Asia-Pacific,Full Diplomatic Relations,High,Yes,Regional Datacenter,
Tajikistan,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Thailand,Asia-Pacific,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Timor-Leste,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Turkmenistan,Asia-Pacific,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Albania,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Andorra,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Austria,Europe,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Belarus,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Bosnia & Herzegovina,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Bulgaria,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Croatia,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Cyprus,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Czechia,Europe,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Estonia,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Germany,Europe,Full Diplomatic Relations,High,Yes,Regional Datacenter,
Greece,Europe,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Holy See (Vatican City),Europe,Full Diplomatic Relations,Critical,Yes,Core Datacenter,
Hungary,Europe,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Italy,Europe,Full Diplomatic Relations,Critical,Yes,Core Datacenter,
Latvia,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Liechtenstein,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Lithuania,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Malta,Europe,Full Diplomatic Relations,High,Yes,Regional Datacenter,
Moldova,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Monaco,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Montenegro,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
North Macedonia,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Poland,Europe,Full Diplomatic Relations,High,Yes,Regional Datacenter,
Portugal,Europe,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Romania,Europe,Full Diplomatic Relations,Medium,Yes,Regional Datacenter,
Russian Federation,Europe,Full Diplomatic Relations (Special Mission),Medium,Yes,Edge/CDN,Special Mission Status
San Marino,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Serbia,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Slovakia,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Slovenia,Europe,Full Diplomatic Relations,Low,Yes,Edge/CDN,
Spain,Europe,Full Diplomatic Relations,High,Yes,Regional Datacenter,
Ukraine,Europe,Full Diplomatic Relations,Medium,Yes,Edge/CDN,
Belgium,Europe,Official (Non-Diplomatic),High,Yes,Regional Datacenter,
France,Europe,Official (Non-Diplomatic),High,Yes,Regional Datacenter,
Canada,Americas,Official (Non-Diplomatic),High,Yes,Regional Datacenter,
United Kingdom,Europe,Official (Non-Diplomatic),High,Yes,Regional Datacenter,
State of Palestine,Middle East,Ambassador Level,Medium,Yes,Edge/CDN,Ambassador-level relations
EOF

# Create regional summary CSV
cat > "${OUTPUT_DIR}/smom_regions_summary_${TIMESTAMP}.csv" << 'EOF'
Region,Total Countries,Full Diplomatic Relations,Official Relations,Ambassador Level,Core Datacenters,Regional Datacenters,Edge/CDN Only
Africa (Sub-Saharan),36,36,0,0,0,4,32
Middle East & North Africa,4,4,0,0,0,4,0
Americas,27,26,1,0,1,3,23
Asia-Pacific,14,14,0,0,0,5,9
Europe,39,35,4,0,2,10,27
Total,120,115,5,1,3,26,91
EOF

echo "CSV files generated:"
echo "  - ${OUTPUT_DIR}/smom_countries_full_${TIMESTAMP}.csv"
echo "  - ${OUTPUT_DIR}/smom_regions_summary_${TIMESTAMP}.csv"
