"""registered_contracts and call_requests

Revision ID: d1be5f227664
Revises: 782ac8fe23c8
Create Date: 2023-04-10 06:37:44.812202

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

# revision identifiers, used by Alembic.
revision = "d1be5f227664"
down_revision = "782ac8fe23c8"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "registered_contracts",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("blockchain", sa.VARCHAR(length=128), nullable=False),
        sa.Column("address", sa.VARCHAR(length=256), nullable=False),
        sa.Column("contract_type", sa.VARCHAR(length=128), nullable=False),
        sa.Column("title", sa.VARCHAR(length=128), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("image_uri", sa.String(), nullable=True),
        sa.Column("moonstream_user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_registered_contracts")),
        sa.UniqueConstraint(
            "blockchain",
            "address",
            "contract_type",
            name=op.f("uq_registered_contracts_blockchain"),
        ),
        sa.UniqueConstraint("id", name=op.f("uq_registered_contracts_id")),
    )
    op.create_index(
        op.f("ix_registered_contracts_address"),
        "registered_contracts",
        ["address"],
        unique=False,
    )
    op.create_index(
        op.f("ix_registered_contracts_blockchain"),
        "registered_contracts",
        ["blockchain"],
        unique=False,
    )
    op.create_index(
        op.f("ix_registered_contracts_contract_type"),
        "registered_contracts",
        ["contract_type"],
        unique=False,
    )
    op.create_index(
        op.f("ix_registered_contracts_moonstream_user_id"),
        "registered_contracts",
        ["moonstream_user_id"],
        unique=False,
    )
    op.create_table(
        "call_requests",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "registered_contract_id", postgresql.UUID(as_uuid=True), nullable=False
        ),
        sa.Column("caller", sa.VARCHAR(length=256), nullable=False),
        sa.Column("moonstream_user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("method", sa.String(), nullable=False),
        sa.Column(
            "parameters", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["registered_contract_id"],
            ["registered_contracts.id"],
            name=op.f("fk_call_requests_registered_contract_id_registered_contracts"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_call_requests")),
        sa.UniqueConstraint("id", name=op.f("uq_call_requests_id")),
    )
    op.create_index(
        op.f("ix_call_requests_caller"), "call_requests", ["caller"], unique=False
    )
    op.create_index(
        op.f("ix_call_requests_expires_at"),
        "call_requests",
        ["expires_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_call_requests_method"), "call_requests", ["method"], unique=False
    )
    op.create_index(
        op.f("ix_call_requests_moonstream_user_id"),
        "call_requests",
        ["moonstream_user_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_call_requests_moonstream_user_id"), table_name="call_requests"
    )
    op.drop_index(op.f("ix_call_requests_method"), table_name="call_requests")
    op.drop_index(op.f("ix_call_requests_expires_at"), table_name="call_requests")
    op.drop_index(op.f("ix_call_requests_caller"), table_name="call_requests")
    op.drop_table("call_requests")
    op.drop_index(
        op.f("ix_registered_contracts_moonstream_user_id"),
        table_name="registered_contracts",
    )
    op.drop_index(
        op.f("ix_registered_contracts_contract_type"), table_name="registered_contracts"
    )
    op.drop_index(
        op.f("ix_registered_contracts_blockchain"), table_name="registered_contracts"
    )
    op.drop_index(
        op.f("ix_registered_contracts_address"), table_name="registered_contracts"
    )
    op.drop_table("registered_contracts")
    # ### end Alembic commands ###
