"""Conversation state the Interaction Standard names (04 §3–4): a thread
resolves (and its notifications stop nagging), a comment is edited or
soft-deleted, and a thread's subject may be the project itself.

Revision ID: 0003_conversation_state
Revises: 0002_interaction
"""

import sqlalchemy as sa
from alembic import op

revision = "0003_conversation_state"
down_revision = "0002_interaction"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # batch mode so the same migration runs on Postgres (prod) and SQLite.
    with op.batch_alter_table("threads") as b:
        b.add_column(sa.Column("resolved", sa.Boolean(), nullable=False,
                               server_default=sa.false()))
        b.add_column(sa.Column("resolved_by", sa.String(120)))
        b.add_column(sa.Column("resolved_at", sa.DateTime(timezone=True)))
        # a project id (80) must fit where a stage id (32) did
        b.alter_column("subject_id", type_=sa.String(80),
                       existing_type=sa.String(32))
    with op.batch_alter_table("comments") as b:
        b.add_column(sa.Column("edited_at", sa.DateTime(timezone=True)))
        b.add_column(sa.Column("deleted_at", sa.DateTime(timezone=True)))
    with op.batch_alter_table("notifications") as b:
        b.add_column(sa.Column("resolved", sa.Boolean(), nullable=False,
                               server_default=sa.false()))


def downgrade() -> None:
    with op.batch_alter_table("notifications") as b:
        b.drop_column("resolved")
    with op.batch_alter_table("comments") as b:
        b.drop_column("deleted_at")
        b.drop_column("edited_at")
    with op.batch_alter_table("threads") as b:
        b.alter_column("subject_id", type_=sa.String(32),
                       existing_type=sa.String(80))
        b.drop_column("resolved_at")
        b.drop_column("resolved_by")
        b.drop_column("resolved")
