"""Migration 0028: Generalize paddle_products → payment_products.

New table supports multiple payment providers (paddle, stripe).
Existing paddle_products rows are copied with provider='paddle'.
The old paddle_products table is kept (no drop) for backwards compatibility.
"""


def up(conn) -> None:
    conn.execute("""
        CREATE TABLE IF NOT EXISTS payment_products (
            id                  INTEGER PRIMARY KEY AUTOINCREMENT,
            provider            TEXT NOT NULL,
            key                 TEXT NOT NULL,
            provider_product_id TEXT NOT NULL,
            provider_price_id   TEXT NOT NULL,
            name                TEXT NOT NULL,
            price_cents         INTEGER NOT NULL,
            currency            TEXT NOT NULL DEFAULT 'EUR',
            billing_type        TEXT NOT NULL,
            created_at          TEXT NOT NULL DEFAULT (datetime('now')),
            UNIQUE(provider, key)
        )
    """)

    # Copy existing paddle_products rows
    conn.execute("""
        INSERT OR IGNORE INTO payment_products
            (provider, key, provider_product_id, provider_price_id, name, price_cents, currency, billing_type, created_at)
        SELECT
            'paddle', key, paddle_product_id, paddle_price_id, name, price_cents, currency, billing_type, created_at
        FROM paddle_products
    """)
