"""songs.py — Simpel sang-router (basis CRUD)."""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from pydantic import BaseModel
from app.core.database import get_db
from app.core.security import get_current_user
from app.models import User, Song

router = APIRouter(prefix="/songs", tags=["songs"])


class SongOut(BaseModel):
    id: str; title: str; artist: str; album: str
    bpm: int; duration_sec: int; file_format: str
    class Config: from_attributes = True


@router.get("/", response_model=list[SongOut])
def list_songs(db: Session = Depends(get_db), me: User = Depends(get_current_user)):
    return db.query(Song).filter(Song.owner_id == me.id).all()


@router.delete("/{song_id}", status_code=204)
def delete_song(song_id: str, db: Session = Depends(get_db), me: User = Depends(get_current_user)):
    song = db.query(Song).filter(Song.id == song_id, Song.owner_id == me.id).first()
    if not song:
        raise HTTPException(404, "Sang ikke fundet")
    db.delete(song)
    db.commit()
