"""
Download management routes for Ohm Stream Downloader API.
"""

from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
from fastapi.templating import Jinja2Templates

from app.download_manager import DownloadManager
from app.models import DownloadRequest
from app.routers.router_auth import get_current_user_from_token

router = APIRouter(prefix="/api/downloads", tags=["downloads"])
templates = Jinja2Templates(directory="templates")


def get_download_manager() -> DownloadManager:
    from main import download_manager
    return download_manager


@router.get("")
async def get_downloads(
    request: Request,
    html: bool = Query(False),
    download_manager: DownloadManager = Depends(get_download_manager),
):
    """Get list of all download tasks. Returns HTML for HTMX."""
    tasks = download_manager.get_all_tasks()
    
    # Force HTML if requested or via HTMX
    if html or request.headers.get("HX-Request"):
        print(f"[DOWNLOADS] HTMX Request detected. Returning HTML for {len(tasks)} tasks.")
        return templates.TemplateResponse(
            "components/downloads_list.html",
            {"request": request, "tasks": tasks}
        )
        
    print(f"[DOWNLOADS] API Request detected. Returning JSON.")
    return {"downloads": tasks}


@router.post("")
async def create_download(
    download_request: DownloadRequest,
    download_manager: DownloadManager = Depends(get_download_manager),
    current_user=Depends(get_current_user_from_token),
):
    """Create a new download task"""
    return download_manager.create_task(download_request)


@router.get("/{task_id}")
async def get_download_status(
    task_id: str,
    download_manager: DownloadManager = Depends(get_download_manager),
):
    """Get status of a specific download task"""
    task = download_manager.get_task(task_id)
    if not task:
        raise HTTPException(status_code=404, detail="Task not found")
    return task


@router.post("/{task_id}/pause")
async def pause_download(
    task_id: str,
    download_manager: DownloadManager = Depends(get_download_manager),
    current_user=Depends(get_current_user_from_token),
):
    """Pause a download task"""
    if download_manager.pause_download(task_id):
        return {"status": "success", "message": "Download paused"}
    raise HTTPException(status_code=400, detail="Failed to pause download")


@router.post("/{task_id}/resume")
async def resume_download(
    task_id: str,
    download_manager: DownloadManager = Depends(get_download_manager),
    current_user=Depends(get_current_user_from_token),
):
    """Resume a paused download task"""
    if download_manager.resume_download(task_id):
        return {"status": "success", "message": "Download resumed"}
    raise HTTPException(status_code=400, detail="Failed to resume download")


@router.delete("/{task_id}")
async def cancel_download(
    task_id: str,
    download_manager: DownloadManager = Depends(get_download_manager),
    current_user=Depends(get_current_user_from_token),
):
    """Cancel and delete a download task"""
    # Use delete_task if cancel_download not available or for full removal
    if hasattr(download_manager, "cancel_download"):
        if download_manager.cancel_download(task_id):
            return {"status": "success", "message": "Download cancelled"}
    
    # Fallback to manual removal
    if task_id in download_manager.tasks:
        del download_manager.tasks[task_id]
        return {"status": "success", "message": "Download removed"}
        
    raise HTTPException(status_code=400, detail="Failed to cancel download")


@router.post("/cleanup")
async def cleanup_completed(
    download_manager: DownloadManager = Depends(get_download_manager),
    current_user=Depends(get_current_user_from_token),
):
    """Remove all completed tasks from the list"""
    if hasattr(download_manager, "cleanup_tasks"):
        count = download_manager.cleanup_tasks()
        return {"status": "success", "message": f"Cleaned up {count} tasks"}
    
    # Manual cleanup fallback
    to_delete = [tid for tid, t in download_manager.tasks.items() if t.status == "completed"]
    for tid in to_delete:
        del download_manager.tasks[tid]
    return {"status": "success", "message": f"Cleaned up {len(to_delete)} tasks"}
