"""services phase 4C task communication timeline

Revision ID: 20260507_task_communication_phase_4c
Revises: 20260506_work_tracker_phase_4b
Create Date: 2026-05-03
"""

from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "20260507_task_communication_phase_4c"
down_revision = "20260506_work_tracker_phase_4b"
branch_labels = None
depends_on = None


def _insp():
    return sa.inspect(op.get_bind())


def _has_table(table_name: str) -> bool:
    return table_name in _insp().get_table_names()


def _has_index(table_name: str, index_name: str) -> bool:
    if not _has_table(table_name):
        return False
    return any(ix.get("name") == index_name for ix in _insp().get_indexes(table_name))


def upgrade() -> None:
    if not _has_table("service_task_comments"):
        op.create_table(
            "service_task_comments",
            sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
            sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
            sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id"), nullable=True),
            sa.Column("subscription_id", sa.Integer(), sa.ForeignKey("client_service_subscriptions.id", ondelete="CASCADE"), nullable=False),
            sa.Column("task_instance_id", sa.Integer(), sa.ForeignKey("client_service_task_instances.id", ondelete="CASCADE"), nullable=False),
            sa.Column("comment_type", sa.String(length=40), nullable=False, server_default="internal_note"),
            sa.Column("visibility", sa.String(length=30), nullable=False, server_default="internal"),
            sa.Column("message", sa.Text(), nullable=False),
            sa.Column("created_by_user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
            sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")),
            sa.Column("updated_at_utc", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")),
            sa.Column("is_deleted", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        )

    index_specs = [
        ("ix_service_task_comments_tenant_id", ["tenant_id"]),
        ("ix_service_task_comments_branch_id", ["branch_id"]),
        ("ix_service_task_comments_subscription_id", ["subscription_id"]),
        ("ix_service_task_comments_task_instance_id", ["task_instance_id"]),
        ("ix_service_task_comments_comment_type", ["comment_type"]),
        ("ix_service_task_comments_visibility", ["visibility"]),
        ("ix_service_task_comments_created_by_user_id", ["created_by_user_id"]),
        ("ix_service_task_comments_is_deleted", ["is_deleted"]),
    ]
    for index_name, columns in index_specs:
        if not _has_index("service_task_comments", index_name):
            op.create_index(index_name, "service_task_comments", columns, unique=False)


def downgrade() -> None:
    if _has_table("service_task_comments"):
        op.drop_table("service_task_comments")
