"""API admin — conta Carbonio individual (Desk Spec 022)."""

from fastapi import APIRouter, Depends, Header, HTTPException

from app.config import settings
from app.services import carbonio, domain_orchestration

router = APIRouter(prefix="/admin/accounts", tags=["admin-accounts"])


def require_api_key(x_api_key: str | None = Header(default=None, alias="X-Api-Key")):
    if x_api_key != settings.admin_api_key:
        raise HTTPException(401, "API key inválida")
    return True


@router.post("/{email}/delete", dependencies=[Depends(require_api_key)])
def delete_account(email: str):
    email = email.lower().strip()
    try:
        return domain_orchestration.delete_carbonio_account(email)
    except ValueError as e:
        raise HTTPException(400, str(e)) from e
    except carbonio.CarbonioError as e:
        raise HTTPException(400, str(e)) from e
