from datetime import datetime from sqlalchemy import JSON, DateTime, Integer, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.core.constants import ActorValue from app.core.db_base import Base from app.core.time import utc_now from app.modules.writebacks.constants import WritebackStatus class OfficialWritebackRun(Base): __tablename__ = "official_writeback_runs" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) code: Mapped[str] = mapped_column(String(64), unique=True, index=True) domain: Mapped[str] = mapped_column(String(128), index=True) record_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True) action: Mapped[str] = mapped_column(String(128), index=True) actor: Mapped[str] = mapped_column(String(128), default=ActorValue.API, index=True) status: Mapped[str] = mapped_column(String(32), default=WritebackStatus.DRAFT, index=True) approval_ticket_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True) idempotency_key: Mapped[str | None] = mapped_column( String(255), nullable=True, unique=True, index=True, ) request_payload: Mapped[dict | None] = mapped_column(JSON, nullable=True) provider_response: Mapped[dict | None] = mapped_column(JSON, nullable=True) error_message: Mapped[str | None] = mapped_column(Text, nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=utc_now, index=True) updated_at: Mapped[datetime] = mapped_column( DateTime, default=utc_now, onupdate=utc_now, ) submitted_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, index=True) sent_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, index=True)