"""Rian's running list: the state he sets on items the sessions raise.

Sources of truth: import/items.json (the items themselves: decisions to make, things to do or
check, issues to address; written only through main/scripts/items.py) and this table (what
rian did about each: decided, done, dismissed, reopened). The /api/items route merges the two
on every request, so a session's new item shows without a deploy and rian's decision is kept
even when the file is rewritten.

`acted_by` keeps the display name as the raw record; `acted_by_id` is the account it was
written from (build plan §2, who-columns rule).
"""

from datetime import datetime

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

from app.models.base import Base


class OwnerItemState(Base):
    __tablename__ = "owner_item_states"

    # The item id from items.json (a slug); one row per item rian has acted on.
    item_id: Mapped[str] = mapped_column(String(80), primary_key=True)
    # open (reopened) | done | dismissed
    status: Mapped[str] = mapped_column(String(12), nullable=False, default="open", server_default="open")
    # The decision text for a "decide" item, or the note for done/dismissed.
    decision: Mapped[str | None] = mapped_column(Text)
    acted_by: Mapped[str | None] = mapped_column(String(120))
    acted_by_id: Mapped[int | None] = mapped_column(ForeignKey("accounts.id"))
    acted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
    )
