#!/bin/bash
# MTGJSON Data Maintenance Script
# This script handles data download, validation, and cleanup
set -e

echo "=== MTGJSON Data Maintenance ==="
echo "Starting maintenance at: $(date)"

# Run the download with sanity checks
echo "Running download with sanity checks..."
docker exec mtgonline_backend python -m app.services.mtgjson_manager --refresh --force

# Verify the download
echo ""
echo "Verifying data integrity..."
docker exec mtgonline_backend python -c "
import sys
sys.path.insert(0, '/app')
from app.services.mtgjson_manager import MTGJSONManager
from pathlib import Path
import asyncio

async def verify():
    manager = MTGJSONManager(Path('/app/data/mtgjson'))
    is_valid, issues = await manager.validate_data_integrity()
    
    if is_valid:
        print('✓ Data integrity check passed')
        return 0
    else:
        print('✗ Data integrity check failed')
        for issue in issues:
            print(f'  - {issue}')
        return 1

sys.exit(asyncio.run(verify()))
"

echo ""
echo "=== Maintenance Complete ==="
