"""Drop deprecated account_id, assigned_at, assigned_by columns from telegram_channels.

These columns are replaced by the ``channel_accounts`` junction table.
All assignment state now lives exclusively in the junction.

Migration is **safe** — the junction table already holds all assignment
data; the deprecated FK columns are no longer written to or read from.
"""

from __future__ import annotations

from repo_core.migrations.builder import Op
from repo_core.migrations.runner import Migration

VERSION = "0005"


class RawSQLOp(Op):
    """A migration operation backed by literal SQL strings."""

    def __init__(self, stmts: list[str]) -> None:
        self._stmts = stmts

    def sql(self) -> list[str]:
        return self._stmts


def get_postgres_migrations() -> list[Migration]:
    """Return ordered PostgreSQL migrations."""
    return [

        Migration(
            version="tg_channel_drop_deprecated_fk",
            description=(
                "Drop account_id, assigned_at, assigned_by columns "
                "from telegram_channels — replaced by channel_accounts junction"
            ),
            up=RawSQLOp(
                stmts=[
                    "DROP INDEX IF EXISTS ix_telegram_channels_account_id",
                    "ALTER TABLE telegram_channels DROP COLUMN IF EXISTS account_id",
                    "ALTER TABLE telegram_channels DROP COLUMN IF EXISTS assigned_at",
                    "ALTER TABLE telegram_channels DROP COLUMN IF EXISTS assigned_by",
                ],
            ),
            down=RawSQLOp(
                stmts=[
                    "ALTER TABLE telegram_channels ADD COLUMN account_id UUID",
                    "ALTER TABLE telegram_channels ADD COLUMN assigned_at TIMESTAMP WITH TIME ZONE",
                    "ALTER TABLE telegram_channels ADD COLUMN assigned_by TEXT",
                    "CREATE INDEX IF NOT EXISTS ix_telegram_channels_account_id ON telegram_channels (account_id)",
                ],
            ),
        ),

    ]
