from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from typing import List, Optional from pydantic import BaseModel from ..db.database import get_db from ..models.lofi import LofiChannel from ..schemas.lofi import LofiChannelResponse class AddLofiChannelRequest(BaseModel): name: str stream_url: str image_path: Optional[str] = "" description: Optional[str] = "" source_platform: Optional[str] = "youtube" router = APIRouter(prefix="/api/lofi", tags=["lofi"]) DEFAULT_CHANNELS = [ {"id": "lofi-girl", "name": "Lofi Girl - beats to relax/study to", "stream_url": "https://www.youtube.com/live/jfKfPfyJRdk", "image_path": "/lofi/rain.jpg", "description": "The original lofi hip hop radio", "source_platform": "youtube", "is_active": True}, {"id": "chillhop", "name": "Chillhop Radio", "stream_url": "https://www.youtube.com/live/5yx6BWtMraY", "image_path": "/lofi/coffee.jpg", "description": "Jazz hop and lofi beats", "source_platform": "youtube", "is_active": True}, {"id": "lofi-hip-hop", "name": "Lofi Hip Hop", "stream_url": "https://www.youtube.com/live/lTRiuFIWV5U", "image_path": "/lofi/night.jpg", "description": "Chill lofi hip hop beats", "source_platform": "youtube", "is_active": True}, ] @router.get("/channels", response_model=List[LofiChannelResponse]) def list_channels(db: Session = Depends(get_db)): _seed_channels(db) channels = db.query(LofiChannel).filter(LofiChannel.is_active == True).all() return [LofiChannelResponse.model_validate(c) for c in channels] @router.post("/add") def add_channel(data: AddLofiChannelRequest, db: Session = Depends(get_db)): import uuid channel_id = str(uuid.uuid4())[:8] existing = db.query(LofiChannel).filter(LofiChannel.name == data.name).first() if existing: raise HTTPException(status_code=400, detail="Channel already exists") channel = LofiChannel( id=channel_id, name=data.name, stream_url=data.stream_url, image_path=data.image_path or "", description=data.description or "", source_platform=data.source_platform or "youtube", ) db.add(channel) db.commit() db.refresh(channel) return LofiChannelResponse.model_validate(channel) def _seed_channels(db: Session): for ch in DEFAULT_CHANNELS: existing = db.query(LofiChannel).filter(LofiChannel.id == ch["id"]).first() if not existing: channel = LofiChannel(**ch) db.add(channel) db.commit()