#!/bin/bash

# Script to import REST Countries JSON data into SQLite
# Usage: ./import_countries.sh countries.json

JSON_FILE="${1:-countries.json}"
DB_FILE="countries.db"

# Check if JSON file exists
if [ ! -f "$JSON_FILE" ]; then
    echo "Error: $JSON_FILE not found!"
    echo "Usage: $0 <path-to-countries.json>"
    exit 1
fi

echo "Creating SQLite database: $DB_FILE"

# Remove existing database if present
rm -f "$DB_FILE"

# Create database and import JSON
sqlite3 "$DB_FILE" << 'EOF'
-- Create table with JSON column
CREATE TABLE countries (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    data JSON NOT NULL
);

-- Enable JSON mode for import
.mode json

-- Read and insert JSON data
.import countries.json countries_temp

-- The import creates a temporary table, now we need to move data
-- Since the JSON file is an array, we need to handle it properly
EOF

# Alternative approach: Use jq to process JSON array and insert row by row
echo "Processing JSON data..."

sqlite3 "$DB_FILE" << 'EOF'
-- If direct import doesn't work, we'll insert via stdin
CREATE TABLE IF NOT EXISTS countries (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    data JSON NOT NULL
);
EOF

# Use jq to split array into individual objects and insert
jq -c '.[]' "$JSON_FILE" | while IFS= read -r country; do
    sqlite3 "$DB_FILE" "INSERT INTO countries (data) VALUES (json('$country'));"
done

echo "Import complete!"
echo "Database created: $DB_FILE"
echo ""
echo "Running sample queries..."
echo ""

# Sample queries
sqlite3 -column -header "$DB_FILE" << 'EOF'
-- Count total countries
SELECT COUNT(*) as total_countries FROM countries;

-- Get countries with German names, population, and timezones
SELECT 
    json_extract(data, '$.name.common') as country_name,
    json_extract(data, '$.name.nativeName.deu.official') as german_name,
    json_extract(data, '$.population') as population,
    json_extract(data, '$.timezones') as timezones
FROM countries
WHERE json_extract(data, '$.name.nativeName.deu.official') IS NOT NULL
LIMIT 10;

-- Get all German translations (from translations field)
SELECT 
    json_extract(data, '$.name.common') as country_name,
    json_extract(data, '$.translations.deu.official') as german_official,
    json_extract(data, '$.translations.deu.common') as german_common
FROM countries
LIMIT 10;
EOF

echo ""
echo "Database ready! You can now query with: sqlite3 $DB_FILE"