"""The discussion workflow (`.logs/planning/discussion-workflow-2026-09-14.md`): the outcome of a
resolution, the closing word, archive, asks, and acknowledgements.

Schema only (build plan §2): nullable columns and two new tables, no data move; one migration
for the whole workflow so there is one rehearsal. Rehearsed up, down and up on `dfp-devdb` from
a copy of the day's dump. Revises `e9f0a1b2c3d4` (the side panel), the single head at the time
of writing; `alembic heads` was checked immediately before this file was written.

1. `threads.outcome` (`done` | `later`, NULL while open), `threads.closing_comment_id` (the
   comment posted as the closing word, a plain integer rather than a foreign key so the two
   tables do not reference each other in a cycle), `threads.archived_at`, `threads.archived_by_id`.
2. `thread_asks`: one hand-off, for one person, with a line saying what; `done_at` and
   `done_by_id` when it is done (the word, if any, is a comment).
3. `comment_acks`: "Got it" on a comment, one row per person and comment.

Revision ID: f0a1b2c3d4e5
Revises: e9f0a1b2c3d4
Create Date: 2026-09-14
"""

import sqlalchemy as sa

from alembic import op

revision = "f0a1b2c3d4e5"
down_revision = "e9f0a1b2c3d4"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column("threads", sa.Column("outcome", sa.String(8), nullable=True))
    op.add_column("threads", sa.Column("closing_comment_id", sa.Integer(), nullable=True))
    op.add_column("threads", sa.Column("archived_at", sa.DateTime(timezone=True), nullable=True))
    op.add_column("threads", sa.Column("archived_by_id", sa.Integer(), sa.ForeignKey("accounts.id"), nullable=True))

    op.create_table(
        "thread_asks",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("thread_id", sa.Integer(), sa.ForeignKey("threads.id"), nullable=False),
        sa.Column("for_id", sa.Integer(), sa.ForeignKey("accounts.id"), nullable=False),
        sa.Column("by_id", sa.Integer(), sa.ForeignKey("accounts.id"), nullable=True),
        sa.Column("note", sa.String(400), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("done_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("done_by_id", sa.Integer(), sa.ForeignKey("accounts.id"), nullable=True),
    )
    op.create_index("ix_thread_asks_thread_id", "thread_asks", ["thread_id"])
    op.create_index("ix_thread_asks_for_id", "thread_asks", ["for_id"])

    op.create_table(
        "comment_acks",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("comment_id", sa.Integer(), sa.ForeignKey("discussion_comments.id"), nullable=False),
        sa.Column("account_id", sa.Integer(), sa.ForeignKey("accounts.id"), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("comment_id", "account_id", name="uq_comment_ack"),
    )
    op.create_index("ix_comment_acks_comment_id", "comment_acks", ["comment_id"])


def downgrade() -> None:
    op.drop_index("ix_comment_acks_comment_id", table_name="comment_acks")
    op.drop_table("comment_acks")
    op.drop_index("ix_thread_asks_for_id", table_name="thread_asks")
    op.drop_index("ix_thread_asks_thread_id", table_name="thread_asks")
    op.drop_table("thread_asks")
    op.drop_column("threads", "archived_by_id")
    op.drop_column("threads", "archived_at")
    op.drop_column("threads", "closing_comment_id")
    op.drop_column("threads", "outcome")
