"""Add invitation_tokens table."""

from alembic import op

revision = "0012"
down_revision = "0011"


def upgrade() -> None:
    op.execute("""
        CREATE TABLE invitation_tokens (
            id SERIAL PRIMARY KEY,
            email TEXT NOT NULL,
            token_hash TEXT NOT NULL UNIQUE,
            invited_by INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
            expires_at TIMESTAMPTZ NOT NULL,
            used BOOLEAN NOT NULL DEFAULT FALSE,
            created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
        )
    """)
    op.execute("CREATE INDEX ix_invitation_tokens_token_hash ON invitation_tokens (token_hash)")
    op.execute("CREATE INDEX ix_invitation_tokens_email ON invitation_tokens (email)")


def downgrade() -> None:
    op.execute("DROP TABLE IF EXISTS invitation_tokens")
