"""Rename lemonsqueezy columns to paddle."""


def up(conn):
    cols = {r[1] for r in conn.execute("PRAGMA table_info(subscriptions)")}
    if "lemonsqueezy_customer_id" in cols:
        conn.execute(
            "ALTER TABLE subscriptions"
            " RENAME COLUMN lemonsqueezy_customer_id TO paddle_customer_id"
        )
        conn.execute(
            "ALTER TABLE subscriptions"
            " RENAME COLUMN lemonsqueezy_subscription_id"
            " TO paddle_subscription_id"
        )
    # Create index on whichever subscription ID column exists
    # (paddle_subscription_id before 0011, provider_subscription_id after)
    conn.execute("DROP INDEX IF EXISTS idx_subscriptions_provider")
    if "paddle_subscription_id" in cols or "lemonsqueezy_subscription_id" in cols:
        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_subscriptions_provider"
            " ON subscriptions(paddle_subscription_id)"
        )
