from sqlalchemy import BigInteger, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON

from app.models.base import BaseModel


class AgentStatusLog(BaseModel):
    __tablename__ = "agent_status_logs"

    agent_id: Mapped[int] = mapped_column(Integer, ForeignKey("user_agents.id", ondelete="CASCADE"), nullable=False, index=True)
    user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
    operator_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True, index=True)
    source: Mapped[str] = mapped_column(String(24), default="system", nullable=False, index=True)
    action: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
    before_status: Mapped[str | None] = mapped_column(String(32), nullable=True)
    after_status: Mapped[str | None] = mapped_column(String(32), nullable=True)
    before_tier_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
    after_tier_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
    reason: Mapped[str] = mapped_column(Text, nullable=False)
    snapshot: Mapped[dict | None] = mapped_column(JSON, nullable=True)
