import logging from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app.core.auth_dependency import require_session from app.db import get_db from app.services import sync from app.services.google_oauth import OAuthNotConnected from app.services.youtube_client import YouTubeAPIError, YouTubeQuotaExceeded logger = logging.getLogger(__name__) router = APIRouter(dependencies=[Depends(require_session)]) @router.post("/sync/subscriptions") def sync_subscriptions(db: Session = Depends(get_db)) -> dict: try: return sync.sync_subscriptions(db) except sync.SyncInProgress: raise HTTPException(status_code=409, detail="Subscriptions sync already in progress") except OAuthNotConnected: raise HTTPException(status_code=400, detail="Google account is not connected") except YouTubeQuotaExceeded: raise HTTPException(status_code=503, detail="YouTube API quota exhausted") except YouTubeAPIError as exc: raise HTTPException(status_code=502, detail=f"YouTube API error: {exc}") @router.post("/sync/videos") def sync_videos(db: Session = Depends(get_db)) -> dict: try: return sync.sync_videos(db) except sync.SyncInProgress: raise HTTPException(status_code=409, detail="Videos sync already in progress") except OAuthNotConnected: raise HTTPException(status_code=400, detail="Google account is not connected") except YouTubeQuotaExceeded: raise HTTPException(status_code=503, detail="YouTube API quota exhausted") except YouTubeAPIError as exc: raise HTTPException(status_code=502, detail=f"YouTube API error: {exc}") @router.get("/sync/status") def sync_status(db: Session = Depends(get_db)) -> dict: return { "subscriptions": sync.get_subscriptions_sync_status(db), "videos": sync.get_videos_sync_status(db), }