"""PostgreSQL migration: drop ``is_platform_manager`` column.

The ``account_type`` field now serves the same purpose — manager accounts
have ``account_type = 'manager'``.  The old boolean column was migrated
in ``0003_account_type`` and is no longer referenced by any code.

This migration removes the now-unused column.
"""

from __future__ import annotations

from typing import Any

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

VERSION = "0004"


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 is_platform_manager cleanup migrations."""
    return [

        # ── 1. Drop the column ──────────────────────────────────────
        Migration(
            version="tg_drop_is_platform_manager_0001",
            description="Drop unused is_platform_manager column from telegram_accounts",
            up=RawSQLOp(
                stmts=[
                    "ALTER TABLE telegram_accounts DROP COLUMN IF EXISTS is_platform_manager",
                ],
            ),
            down=RawSQLOp(
                stmts=[
                    "ALTER TABLE telegram_accounts "
                    "ADD COLUMN is_platform_manager BOOLEAN DEFAULT FALSE",
                    # Re-populate from account_type for any rows that still exist
                    "UPDATE telegram_accounts "
                    "SET is_platform_manager = TRUE "
                    "WHERE account_type = 'manager'",
                ],
            ),
        ),
    ]
