# @File: backend/models/whmcs_api_logs.py
# @Desc: SQLAlchemy model for whmcs_api_logs. Every WHMCS API call is recorded here
#        so admins can audit, debug, and monitor WHMCS integration activity.
from datetime import datetime

from sqlalchemy import Column, DateTime, Integer, String, Text
from sqlalchemy.sql import func

from models.base import Base


class WhmcsApiLog(Base):
    """Audit log of WHMCS API calls.

    We never store secrets or full PII — only the action name, the authenticated
    user email (or None for anonymous calls), status, upstream http status,
    duration, and a short error message if the call failed.
    """

    __tablename__ = "whmcs_api_logs"

    id = Column(Integer, primary_key=True, autoincrement=True, index=True)
    action = Column(String(128), nullable=False, index=True)
    user_email = Column(String(255), nullable=True, index=True)
    status = Column(String(16), nullable=False, index=True)  # "success" | "error"
    http_status = Column(Integer, nullable=True)
    duration_ms = Column(Integer, nullable=True)
    error = Column(Text, nullable=True)
    created_at = Column(DateTime, server_default=func.now(), nullable=False, index=True)