"""Phase 7.1 — Membership lifecycle schema (additive)."""
from alembic import op
import sqlalchemy as sa

revision = "0002_phase_71_membership"
down_revision = "0001_initial"
branch_labels = None
depends_on = None


def upgrade():
    op.add_column(
        "members",
        sa.Column("activated_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "members",
        sa.Column("membership_started_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "members",
        sa.Column("membership_expires_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "members",
        sa.Column("frozen_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "members", sa.Column("freeze_reason", sa.String(length=500), nullable=True)
    )
    op.add_column(
        "members",
        sa.Column("cancelled_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "members", sa.Column("cancel_reason", sa.String(length=500), nullable=True)
    )
    op.add_column(
        "members",
        sa.Column("expired_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "members",
        sa.Column("transferred_to_member_id", sa.CHAR(length=36), nullable=True),
    )
    op.create_index(
        "ix_members_tenant_expires",
        "members",
        ["tenant_id", "membership_expires_at"],
    )

    op.create_table(
        "membership_lifecycle_events",
        sa.Column("id", sa.CHAR(length=36), primary_key=True),
        sa.Column("tenant_id", sa.CHAR(length=36), nullable=False),
        sa.Column("member_id", sa.CHAR(length=36), nullable=False),
        sa.Column("action", sa.String(length=30), nullable=False),
        sa.Column("from_status", sa.String(length=30), nullable=False),
        sa.Column("to_status", sa.String(length=30), nullable=False),
        sa.Column("reason", sa.String(length=500), nullable=True),
        sa.Column("metadata_json", sa.JSON(), nullable=True),
        sa.Column("actor_user_id", sa.String(length=100), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    )
    op.create_index(
        "ix_membership_lifecycle_member",
        "membership_lifecycle_events",
        ["tenant_id", "member_id"],
    )
    op.create_index(
        "ix_membership_lifecycle_action",
        "membership_lifecycle_events",
        ["tenant_id", "action"],
    )
    op.create_index(
        "ix_membership_lifecycle_created",
        "membership_lifecycle_events",
        ["tenant_id", "created_at"],
    )


def downgrade():
    op.drop_index(
        "ix_membership_lifecycle_created", table_name="membership_lifecycle_events"
    )
    op.drop_index(
        "ix_membership_lifecycle_action", table_name="membership_lifecycle_events"
    )
    op.drop_index(
        "ix_membership_lifecycle_member", table_name="membership_lifecycle_events"
    )
    op.drop_table("membership_lifecycle_events")
    op.drop_index("ix_members_tenant_expires", table_name="members")
    op.drop_column("members", "transferred_to_member_id")
    op.drop_column("members", "expired_at")
    op.drop_column("members", "cancel_reason")
    op.drop_column("members", "cancelled_at")
    op.drop_column("members", "freeze_reason")
    op.drop_column("members", "frozen_at")
    op.drop_column("members", "membership_expires_at")
    op.drop_column("members", "membership_started_at")
    op.drop_column("members", "activated_at")
