from fastapi import APIRouter, Depends, Request, Response
from sqlalchemy.orm import Session as DBSession
from app.core.database import get_db
from app.models.session import Session
from app.core.auth import SESSION_COOKIE_NAME, clear_session_cookie

router = APIRouter()

@router.delete("/sessions/me")
def delete_own_session(request: Request, response: Response, db: DBSession = Depends(get_db)):
    token = request.cookies.get(SESSION_COOKIE_NAME)
    if not token:
        clear_session_cookie(response)
        return {"detail": "No active session"}
    session = db.query(Session).filter_by(token=token).first()
    if session:
        db.delete(session)
        db.commit()
    clear_session_cookie(response)
    return {"detail": "Session deleted"}
