from sqlalchemy import String, DateTime, Integer, Text, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from datetime import datetime
from app.database import Base


class AdminAuditLog(Base):
    """
    Audit log for tracking all admin actions on the platform.
    Every admin action should be logged for accountability and debugging.
    """
    __tablename__ = "admin_audit_logs"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)

    # Who performed the action
    admin_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
    admin_username: Mapped[str] = mapped_column(String(50), nullable=False)

    # What action was performed
    action: Mapped[str] = mapped_column(String(50), nullable=False, index=True)
    # Action codes:
    # - DATA_WIPE: Database wipe executed
    # - DATA_SEED: Database seeded with test data
    # - SIMULATION_START: Activity simulation started
    # - SIMULATION_STOP: Activity simulation stopped
    # - USER_STATUS_CHANGE: User enabled/disabled
    # - USER_BALANCE_ADJUST: User balance adjusted
    # - USER_ADMIN_GRANT: Admin privileges granted
    # - USER_ADMIN_REVOKE: Admin privileges revoked
    # - USER_UPDATE: User details updated
    # - SETTINGS_UPDATE: Platform settings changed
    # - EVENT_CREATE: Sport event created
    # - EVENT_UPDATE: Sport event updated
    # - EVENT_DELETE: Sport event deleted

    # Target of the action (if applicable)
    target_type: Mapped[str | None] = mapped_column(String(50), nullable=True)  # e.g., "user", "event", "bet"
    target_id: Mapped[int | None] = mapped_column(Integer, nullable=True)

    # Description of the action
    description: Mapped[str] = mapped_column(String(500), nullable=False)

    # Additional details as JSON string
    details: Mapped[str | None] = mapped_column(Text, nullable=True)

    # IP address of the admin (for security)
    ip_address: Mapped[str | None] = mapped_column(String(45), nullable=True)

    # Timestamp
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, index=True)

    # Relationship to admin user
    admin: Mapped["User"] = relationship("User", foreign_keys=[admin_id])
