"""Initial schema — licenses + gumroad_events.

Revision ID: 0001_initial
Revises:
Create Date: 2026-05-14
"""

from __future__ import annotations

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision: str = "0001_initial"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "licenses",
        sa.Column("license_key", sa.String(), primary_key=True),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("email", sa.String(), nullable=False),
        sa.Column("tier", sa.String(), nullable=False),
        sa.Column("issued_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("blob", sa.String(), nullable=False),
        sa.Column("source", sa.String(), nullable=False),
        sa.Column("source_order_id", sa.String(), nullable=True),
        sa.Column("promotion", sa.String(), nullable=True),
        sa.Column("amount_paid", sa.Numeric(10, 2), nullable=True),
        sa.Column("currency", sa.String(length=3), server_default=sa.text("'USD'"), nullable=True),
        sa.Column("revoked_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("notes", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.UniqueConstraint("source", "source_order_id", name="uq_licenses_source_order"),
    )
    op.create_index(
        "ix_licenses_email_lower",
        "licenses",
        [sa.text("lower(email)")],
    )
    op.create_index(
        "ix_licenses_expires_active",
        "licenses",
        ["expires_at"],
        postgresql_where=sa.text("revoked_at IS NULL"),
    )

    op.create_table(
        "gumroad_events",
        sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
        sa.Column("received_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("event_type", sa.String(), nullable=False),
        sa.Column("order_id", sa.String(), nullable=True),
        sa.Column("raw_payload", postgresql.JSONB(), nullable=False),
        sa.Column("processed", sa.Boolean(), server_default=sa.text("false"), nullable=False),
        sa.Column("error", sa.String(), nullable=True),
    )
    op.create_index("ix_gumroad_events_order_id", "gumroad_events", ["order_id"])
    op.create_index(
        "ix_gumroad_events_unprocessed",
        "gumroad_events",
        ["received_at"],
        postgresql_where=sa.text("processed = false"),
    )


def downgrade() -> None:
    op.drop_index("ix_gumroad_events_unprocessed", table_name="gumroad_events")
    op.drop_index("ix_gumroad_events_order_id", table_name="gumroad_events")
    op.drop_table("gumroad_events")
    op.drop_index("ix_licenses_expires_active", table_name="licenses")
    op.drop_index("ix_licenses_email_lower", table_name="licenses")
    op.drop_table("licenses")
