from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session

from app.dependencies import get_db, get_current_auth_session
from app.schemas.public_ip import PublicIPCheckResponse
from app.config import get_settings
from app.services.auth import AuthenticatedSession
from app.services.public_ip import check_public_ipv4_and_notify

router = APIRouter(tags=["public-ip"])


@router.get("/public-ip/check", response_model=PublicIPCheckResponse)
def run_public_ip_check(
    session: Session = Depends(get_db),
    current_auth: AuthenticatedSession | None = Depends(get_current_auth_session),
) -> PublicIPCheckResponse:
    if current_auth is None:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="authentication required")

    result = check_public_ipv4_and_notify(session, bootstrap_settings=get_settings())
    return PublicIPCheckResponse(
        status=result.status,
        checked_at=result.checked_at,
        changed=result.changed,
    )