"""
MTGJSON Data Refresh Router

Provides endpoints for triggering dataset downloads and managing refresh operations.
"""
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status
from typing import Optional

from app.core.database import get_db
from app.core.security import get_current_user
from app.services.mtgjson_manager import MTGJSONManager

router = APIRouter(prefix="/mtgjson", tags=["MTGJSON Data"])


@router.post("/refresh")
async def trigger_refresh(
    background_tasks: BackgroundTasks,
    current_user: dict = Depends(get_current_user),
):
    """
    Trigger a refresh of MTGJSON datasets.
    
    Downloads files from MTGJSON API and upserts data into PostgreSQL.
    Requires Admin privileges.
    
    The refresh runs in the background and may take several minutes.
    """
    # Check if current user is admin
    if current_user.get("privlevel") != "Admin":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Admin privileges required",
        )
    
    # Create manager and trigger refresh in background
    manager = MTGJSONManager()
    background_tasks.add_task(manager.download_and_refresh, force=False)
    
    return {
        "message": "Refresh initiated in background",
        "status": "started",
    }


@router.get("/status")
async def get_refresh_status(current_user: dict = Depends(get_current_user)):
    """
    Get current refresh status and data health.
    
    Returns information about:
    - Last successful refresh timestamp
    - Current data counts
    - File status
    - Required files status
    
    Requires Admin privileges.
    """
    # Check if current user is admin
    if current_user.get("privlevel") != "Admin":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Admin privileges required",
        )
    
    manager = MTGJSONManager()
    return await manager.get_health_status()


@router.post("/verify")
async def verify_files(current_user: dict = Depends(get_current_user)):
    """
    Verify the integrity of downloaded MTGJSON files.
    
    Checks that all required files exist and are valid.
    Requires Admin privileges.
    """
    # Check if current user is admin
    if current_user.get("privlevel") != "Admin":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Admin privileges required",
        )
    
    manager = MTGJSONManager()
    all_valid, errors = await manager.verify_files()
    
    if not all_valid:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail={
                "valid": False,
                "errors": errors,
                "message": "Some files failed verification",
            },
        )
    
    return {
        "valid": True,
        "message": "All files verified successfully",
    }
