"""Public (unauthenticated + user-authenticated) endpoints."""
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from core.auth import get_current_user
from db.session import get_session
from routes.admin_users import get_settings_doc

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


@router.get("/shared-xtream")
def shared_xtream(db: Session = Depends(get_session)):
    """Legacy single-Xtream shared creds endpoint (public)."""
    settings = get_settings_doc(db)
    return {
        "server": settings.get("shared_xtream_server", ""),
        "username": settings.get("shared_xtream_username", ""),
        "password": settings.get("shared_xtream_password", ""),
        "configured": bool(
            settings.get("shared_xtream_server")
            and settings.get("shared_xtream_username")
            and settings.get("shared_xtream_password")
        ),
    }


@router.get("/playback-mode")
def playback_mode(db: Session = Depends(get_session)):
    """Public read-only view of admin's playback preference."""
    settings = get_settings_doc(db)
    return {
        "external_player_enabled": bool(
            settings.get("external_player_enabled", False)
        ),
        "inline_preview_enabled": bool(
            settings.get("inline_preview_enabled", True)
        ),
    }


# Authenticated endpoint for multi-profile picker.
auth_router = APIRouter(prefix="/api", tags=["shared"])


@auth_router.get("/shared-profiles")
def list_shared_profiles(
    user=Depends(get_current_user),
    db: Session = Depends(get_session),
):
    settings = get_settings_doc(db)
    return {"profiles": settings.get("shared_profiles") or []}
