#!/bin/bash
# MTGJSON Data Download and Import Script
# Downloads all MTGJSON data and creates a complete PostgreSQL database

set -e

MTGDATA_DIR="/home/wall-o/projects/mtgonline/backend/mtgdata"
TEMP_DIR="$MTGDATA_DIR/temp"
PSQL_DB="mtgdata"
PSQL_USER="cockatrice"
PSQL_HOST="mtgonline_postgres_mtgdata"

echo "=== MTGJSON Complete Data Download and Import ==="
echo ""

# Create directories
mkdir -p "$MTGDATA_DIR" "$TEMP_DIR"

# Download all requested files
echo "Downloading MTGJSON data files..."
echo ""

# Main cards database (PSQL format - no conversion needed)
echo "1. Downloading AllPrintings.psql.gz (cards database)..."
curl -sL "https://mtgjson.com/api/v5/AllPrintings.psql.gz" -o "$MTGDATA_DIR/AllPrintings.psql.gz"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/AllPrintings.psql.gz | cut -f1))"

# Set files
echo "2. Downloading AllSetFiles.zip..."
curl -sL "https://mtgjson.com/api/v5/AllSetFiles.zip" -o "$MTGDATA_DIR/AllSetFiles.zip"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/AllSetFiles.zip | cut -f1))"

# Deck files
echo "3. Downloading AllDeckFiles.zip..."
curl -sL "https://mtgjson.com/api/v5/AllDeckFiles.zip" -o "$MTGDATA_DIR/AllDeckFiles.zip"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/AllDeckFiles.zip | cut -f1))"

# Identifiers
echo "4. Downloading AllIdentifiers.json.gz..."
curl -sL "https://mtgjson.com/api/v5/AllIdentifiers.json.gz" -o "$MTGDATA_DIR/AllIdentifiers.json.gz"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/AllIdentifiers.json.gz | cut -f1))"

# Card Types
echo "5. Downloading CardTypes.json.gz..."
curl -sL "https://mtgjson.com/api/v5/CardTypes.json.gz" -o "$MTGDATA_DIR/CardTypes.json.gz"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/CardTypes.json.gz | cut -f1))"

# Deck List
echo "6. Downloading DeckList.json.gz..."
curl -sL "https://mtgjson.com/api/v5/DeckList.json.gz" -o "$MTGDATA_DIR/DeckList.json.gz"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/DeckList.json.gz | cut -f1))"

# Keywords
echo "7. Downloading Keywords.json.gz..."
curl -sL "https://mtgjson.com/api/v5/Keywords.json.gz" -o "$MTGDATA_DIR/Keywords.json.gz"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/Keywords.json.gz | cut -f1))"

# Set List
echo "8. Downloading SetList.json.gz..."
curl -sL "https://mtgjson.com/api/v5/SetList.json.gz" -o "$MTGDATA_DIR/SetList.json.gz"
echo "   ✓ Downloaded ($(du -h $MTGDATA_DIR/SetList.json.gz | cut -f1))"

echo ""
echo "All files downloaded successfully!"
echo ""
echo "Total size: $(du -sh $MTGDATA_DIR | cut -f1)"
echo ""

# Verify all files exist
echo "Verifying downloads..."
for file in AllPrintings.psql.gz AllSetFiles.zip AllDeckFiles.zip AllIdentifiers.json.gz CardTypes.json.gz DeckList.json.gz Keywords.json.gz SetList.json.gz; do
    if [ -f "$MTGDATA_DIR/$file" ]; then
        echo "   ✓ $file"
    else
        echo "   ✗ $file (MISSING)"
        exit 1
    fi
done

echo ""
echo "=== Downloads Complete ==="
echo ""
echo "Next steps:"
echo "1. The AllPrintings.psql.gz file can be directly imported into PostgreSQL"
echo "2. Extract and process the zip files for additional data"
echo "3. Import all data into the mtgdata database"
