from sqlalchemy import DateTime, ForeignKey, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base
from app.utils.id_gen import generate_id


class ChatGenerationTaskEvent(Base):
    """Append-only event log for project-independent chat generation tasks."""

    __tablename__ = "chat_generation_task_events"

    id: Mapped[str] = mapped_column(String(32), primary_key=True, default=generate_id)
    task_id: Mapped[str] = mapped_column(
        String(32), ForeignKey("chat_generation_tasks.id", ondelete="CASCADE"), index=True
    )
    generation_mode: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
    event_type: Mapped[str] = mapped_column(String(64), index=True)
    from_status: Mapped[str | None] = mapped_column(String(32), nullable=True)
    to_status: Mapped[str | None] = mapped_column(String(32), nullable=True)
    from_stage: Mapped[str | None] = mapped_column(String(32), nullable=True)
    to_stage: Mapped[str | None] = mapped_column(String(32), nullable=True)
    message: Mapped[str | None] = mapped_column(String(512), nullable=True)
    detail_json: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_at = mapped_column(DateTime(timezone=True), server_default=func.now(), index=True)
