"""Initialize RoboSystems IAM Schema

Revision ID: c2f378adcdb4
Revises:
Create Date: 2025-08-15 19:27:56.932141

"""

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

# revision identifiers, used by Alembic.
revision = "c2f378adcdb4"
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.create_table(
    "connection_credentials",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("connection_id", sa.String(), nullable=False),
    sa.Column("provider", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("encrypted_credentials", sa.Text(), nullable=False),
    sa.Column("expires_at", sa.DateTime(), nullable=True),
    sa.Column("is_active", sa.Boolean(), nullable=False),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    op.f("ix_connection_credentials_connection_id"),
    "connection_credentials",
    ["connection_id"],
    unique=False,
  )
  op.create_index(
    op.f("ix_connection_credentials_provider"),
    "connection_credentials",
    ["provider"],
    unique=False,
  )
  op.create_index(
    op.f("ix_connection_credentials_user_id"),
    "connection_credentials",
    ["user_id"],
    unique=False,
  )
  op.create_table(
    "graph_usage_tracking",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("event_type", sa.String(), nullable=False),
    sa.Column("operation_type", sa.String(), nullable=True),
    sa.Column("graph_tier", sa.String(), nullable=False),
    sa.Column("instance_id", sa.String(), nullable=True),
    sa.Column("region", sa.String(), nullable=True),
    sa.Column("storage_bytes", sa.Float(), nullable=True),
    sa.Column("storage_gb", sa.Float(), nullable=True),
    sa.Column("storage_delta_gb", sa.Float(), nullable=True),
    sa.Column("credits_consumed", sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column("credit_multiplier", sa.Numeric(precision=3, scale=1), nullable=True),
    sa.Column("base_credit_cost", sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column("duration_ms", sa.Integer(), nullable=True),
    sa.Column("memory_mb", sa.Float(), nullable=True),
    sa.Column("cpu_percent", sa.Float(), nullable=True),
    sa.Column("request_size_kb", sa.Float(), nullable=True),
    sa.Column("response_size_kb", sa.Float(), nullable=True),
    sa.Column("status_code", sa.Integer(), nullable=True),
    sa.Column("cached_operation", sa.Boolean(), nullable=True),
    sa.Column("storage_overage_gb", sa.Float(), nullable=True),
    sa.Column("estimated_cost_cents", sa.Integer(), nullable=True),
    sa.Column("error_type", sa.String(), nullable=True),
    sa.Column("error_message", sa.Text(), nullable=True),
    sa.Column("metadata", sa.Text(), nullable=True),
    sa.Column("user_agent", sa.String(), nullable=True),
    sa.Column("ip_address", sa.String(), nullable=True),
    sa.Column("recorded_at", sa.DateTime(), nullable=False),
    sa.Column("billing_year", sa.Integer(), nullable=False),
    sa.Column("billing_month", sa.Integer(), nullable=False),
    sa.Column("billing_day", sa.Integer(), nullable=False),
    sa.Column("billing_hour", sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    "idx_billing_period",
    "graph_usage_tracking",
    ["billing_year", "billing_month", "user_id"],
    unique=False,
  )
  op.create_index(
    "idx_credits_consumed", "graph_usage_tracking", ["credits_consumed"], unique=False
  )
  op.create_index(
    "idx_event_type_time",
    "graph_usage_tracking",
    ["event_type", "recorded_at"],
    unique=False,
  )
  op.create_index(
    "idx_graph_tier_time",
    "graph_usage_tracking",
    ["graph_tier", "recorded_at"],
    unique=False,
  )
  op.create_index(
    "idx_performance_analysis",
    "graph_usage_tracking",
    ["operation_type", "duration_ms", "recorded_at"],
    unique=False,
  )
  op.create_index(
    "idx_storage_billing",
    "graph_usage_tracking",
    ["user_id", "graph_id", "billing_year", "billing_month"],
    unique=False,
  )
  op.create_index(
    "idx_user_graph_time",
    "graph_usage_tracking",
    ["user_id", "graph_id", "recorded_at"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_billing_day"),
    "graph_usage_tracking",
    ["billing_day"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_billing_month"),
    "graph_usage_tracking",
    ["billing_month"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_billing_year"),
    "graph_usage_tracking",
    ["billing_year"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_event_type"),
    "graph_usage_tracking",
    ["event_type"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_graph_id"),
    "graph_usage_tracking",
    ["graph_id"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_graph_tier"),
    "graph_usage_tracking",
    ["graph_tier"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_recorded_at"),
    "graph_usage_tracking",
    ["recorded_at"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_usage_tracking_user_id"),
    "graph_usage_tracking",
    ["user_id"],
    unique=False,
  )
  op.create_table(
    "graphs",
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("graph_name", sa.String(), nullable=False),
    sa.Column("graph_type", sa.String(), nullable=False),
    sa.Column("base_schema", sa.String(), nullable=True),
    sa.Column(
      "schema_extensions", postgresql.JSONB(astext_type=sa.Text()), nullable=False
    ),
    sa.Column("graph_instance_id", sa.String(), nullable=False),
    sa.Column("graph_cluster_region", sa.String(), nullable=True),
    sa.Column("graph_tier", sa.String(), nullable=False),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.Column("graph_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.CheckConstraint("graph_type IN ('generic', 'entity')", name="check_graph_type"),
    sa.PrimaryKeyConstraint("graph_id"),
  )
  op.create_index("idx_graphs_instance", "graphs", ["graph_instance_id"], unique=False)
  op.create_index(
    "idx_graphs_schema_extensions",
    "graphs",
    ["schema_extensions"],
    unique=False,
    postgresql_using="gin",
  )
  op.create_index("idx_graphs_tier", "graphs", ["graph_tier"], unique=False)
  op.create_index("idx_graphs_type", "graphs", ["graph_type"], unique=False)
  op.create_index(
    op.f("ix_graphs_graph_instance_id"), "graphs", ["graph_instance_id"], unique=False
  )
  op.create_table(
    "users",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("email", sa.String(), nullable=False),
    sa.Column("name", sa.String(), nullable=False),
    sa.Column("password_hash", sa.String(), nullable=False),
    sa.Column("is_active", sa.Boolean(), nullable=False),
    sa.Column("email_verified", sa.Boolean(), nullable=False),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(op.f("ix_users_email"), "users", ["email"], unique=True)
  op.create_table(
    "graph_backups",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("database_name", sa.String(), nullable=False),
    sa.Column("backup_type", sa.String(), nullable=False),
    sa.Column("status", sa.String(), nullable=False),
    sa.Column("s3_bucket", sa.String(), nullable=False),
    sa.Column("s3_key", sa.String(), nullable=False),
    sa.Column("s3_metadata_key", sa.String(), nullable=True),
    sa.Column("original_size_bytes", sa.Integer(), nullable=False),
    sa.Column("compressed_size_bytes", sa.Integer(), nullable=False),
    sa.Column("encrypted_size_bytes", sa.Integer(), nullable=False),
    sa.Column("compression_ratio", sa.Float(), nullable=False),
    sa.Column("node_count", sa.Integer(), nullable=False),
    sa.Column("relationship_count", sa.Integer(), nullable=False),
    sa.Column("database_version", sa.String(), nullable=True),
    sa.Column("backup_duration_seconds", sa.Float(), nullable=False),
    sa.Column("checksum", sa.String(), nullable=True),
    sa.Column("encryption_enabled", sa.Boolean(), nullable=False),
    sa.Column("compression_enabled", sa.Boolean(), nullable=False),
    sa.Column("error_message", sa.Text(), nullable=True),
    sa.Column("retry_count", sa.Integer(), nullable=False),
    sa.Column("backup_metadata", sa.JSON(), nullable=True),
    sa.Column("started_at", sa.DateTime(), nullable=True),
    sa.Column("completed_at", sa.DateTime(), nullable=True),
    sa.Column("expires_at", sa.DateTime(), nullable=True),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.Column("created_by_user_id", sa.String(), nullable=True),
    sa.ForeignKeyConstraint(
      ["created_by_user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    op.f("ix_graph_backups_created_by_user_id"),
    "graph_backups",
    ["created_by_user_id"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_backups_database_name"),
    "graph_backups",
    ["database_name"],
    unique=False,
  )
  op.create_index(
    op.f("ix_graph_backups_graph_id"), "graph_backups", ["graph_id"], unique=False
  )
  op.create_index(
    op.f("ix_graph_backups_status"), "graph_backups", ["status"], unique=False
  )
  op.create_table(
    "graph_credits",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("current_balance", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column("monthly_allocation", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column("credit_multiplier", sa.Numeric(precision=3, scale=1), nullable=False),
    sa.Column("storage_limit_gb", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column("storage_override_gb", sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column("auto_expand_enabled", sa.Boolean(), nullable=False),
    sa.Column("last_storage_warning_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column(
      "storage_warning_threshold", sa.Numeric(precision=3, scale=2), nullable=False
    ),
    sa.Column("billing_admin_id", sa.String(), nullable=False),
    sa.Column("last_allocation_date", sa.DateTime(timezone=True), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(
      ["billing_admin_id"],
      ["users.id"],
    ),
    sa.ForeignKeyConstraint(
      ["graph_id"],
      ["graphs.graph_id"],
    ),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
    sa.UniqueConstraint("graph_id"),
  )
  op.create_index(
    "idx_graph_credits_billing_admin",
    "graph_credits",
    ["billing_admin_id"],
    unique=False,
  )
  op.create_index(
    "idx_graph_credits_graph_id", "graph_credits", ["graph_id"], unique=False
  )
  op.create_index(
    "idx_graph_credits_storage_monitoring",
    "graph_credits",
    ["storage_limit_gb"],
    unique=False,
  )
  op.create_index(
    "idx_graph_credits_user_id", "graph_credits", ["user_id"], unique=False
  )
  op.create_table(
    "graph_subscriptions",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("plan_name", sa.String(), nullable=True),
    sa.Column(
      "status",
      postgresql.ENUM(
        "ACTIVE", "PAST_DUE", "CANCELED", "UNPAID", name="subscriptionstatus"
      ),
      nullable=False,
    ),
    sa.Column("current_period_start", sa.DateTime(timezone=True), nullable=True),
    sa.Column("current_period_end", sa.DateTime(timezone=True), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_table(
    "user_api_keys",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("name", sa.String(), nullable=False),
    sa.Column("key_hash", sa.String(), nullable=False),
    sa.Column("prefix", sa.String(), nullable=False),
    sa.Column("is_active", sa.Boolean(), nullable=False),
    sa.Column("description", sa.Text(), nullable=True),
    sa.Column("last_used_at", sa.DateTime(), nullable=True),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    "idx_user_api_keys_hash_active",
    "user_api_keys",
    ["key_hash", "is_active"],
    unique=False,
  )
  op.create_index(
    "idx_user_api_keys_last_used", "user_api_keys", ["last_used_at"], unique=False
  )
  op.create_index(
    "idx_user_api_keys_prefix_active",
    "user_api_keys",
    ["prefix", "is_active"],
    unique=False,
  )
  op.create_index(
    op.f("ix_user_api_keys_key_hash"), "user_api_keys", ["key_hash"], unique=True
  )
  op.create_index(
    op.f("ix_user_api_keys_prefix"), "user_api_keys", ["prefix"], unique=False
  )
  op.create_index(
    op.f("ix_user_api_keys_user_id"), "user_api_keys", ["user_id"], unique=False
  )
  op.create_table(
    "user_graphs",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("role", sa.String(), nullable=False),
    sa.Column("is_selected", sa.Boolean(), nullable=False),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(
      ["graph_id"],
      ["graphs.graph_id"],
    ),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
    sa.UniqueConstraint("user_id", "graph_id", name="_user_graph_uc"),
  )
  op.create_index(
    "idx_user_graphs_user_graph_id",
    "user_graphs",
    ["user_id", "graph_id"],
    unique=False,
  )
  op.create_index(
    "idx_user_graphs_user_selected",
    "user_graphs",
    ["user_id", "is_selected"],
    unique=False,
  )
  op.create_index(
    op.f("ix_user_graphs_graph_id"), "user_graphs", ["graph_id"], unique=False
  )
  op.create_index(
    op.f("ix_user_graphs_user_id"), "user_graphs", ["user_id"], unique=False
  )
  op.create_table(
    "user_limits",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("max_user_graphs", sa.Integer(), nullable=False),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
    sa.UniqueConstraint("user_id"),
  )
  op.create_table(
    "user_repository",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column(
      "repository_type",
      sa.Enum("SEC", "INDUSTRY", "ECONOMIC", name="repositorytype"),
      nullable=False,
    ),
    sa.Column("repository_name", sa.String(), nullable=False),
    sa.Column(
      "access_level",
      sa.Enum("NONE", "READ", "WRITE", "ADMIN", name="repositoryaccesslevel"),
      nullable=False,
    ),
    sa.Column(
      "repository_plan",
      sa.Enum("STARTER", "ADVANCED", "UNLIMITED", name="repositoryplan"),
      nullable=False,
    ),
    sa.Column("is_active", sa.Boolean(), nullable=False),
    sa.Column("activated_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("monthly_price_cents", sa.Integer(), nullable=False),
    sa.Column("billing_cycle_day", sa.Integer(), nullable=True),
    sa.Column("last_billed_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("next_billing_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("monthly_credit_allocation", sa.Integer(), nullable=False),
    sa.Column("granted_by", sa.String(), nullable=True),
    sa.Column("granted_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("graph_instance_id", sa.String(), nullable=False),
    sa.Column("graph_cluster_region", sa.String(), nullable=True),
    sa.Column("instance_tier", sa.String(), nullable=False),
    sa.Column("read_preference", sa.String(), nullable=False),
    sa.Column("access_scope", sa.String(), nullable=True),
    sa.Column("quota_limits", sa.String(), nullable=True),
    sa.Column("extra_metadata", sa.Text(), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(
      ["granted_by"],
      ["users.id"],
    ),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
    sa.UniqueConstraint(
      "user_id", "repository_type", "repository_name", name="uq_user_repo_access"
    ),
  )
  op.create_index(
    "idx_user_shared_repo_billing",
    "user_repository",
    ["next_billing_at", "is_active"],
    unique=False,
  )
  op.create_index(
    "idx_user_shared_repo_expires",
    "user_repository",
    ["expires_at", "is_active"],
    unique=False,
  )
  op.create_index(
    "idx_user_shared_repo_plan",
    "user_repository",
    ["repository_plan", "is_active"],
    unique=False,
  )
  op.create_index(
    "idx_user_shared_repo_type_level",
    "user_repository",
    ["repository_type", "access_level", "is_active"],
    unique=False,
  )
  op.create_index(
    "idx_user_shared_repo_user_active",
    "user_repository",
    ["user_id", "is_active"],
    unique=False,
  )
  op.create_table(
    "user_usage_tracking",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("usage_type", sa.String(), nullable=False),
    sa.Column("occurred_at", sa.DateTime(), nullable=False),
    sa.Column("endpoint", sa.String(), nullable=True),
    sa.Column("graph_id", sa.String(), nullable=True),
    sa.Column("resource_count", sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    "idx_usage_type_time",
    "user_usage_tracking",
    ["usage_type", "occurred_at"],
    unique=False,
  )
  op.create_index(
    "idx_user_usage_type_time",
    "user_usage_tracking",
    ["user_id", "usage_type", "occurred_at"],
    unique=False,
  )
  op.create_index(
    op.f("ix_user_usage_tracking_occurred_at"),
    "user_usage_tracking",
    ["occurred_at"],
    unique=False,
  )
  op.create_index(
    op.f("ix_user_usage_tracking_usage_type"),
    "user_usage_tracking",
    ["usage_type"],
    unique=False,
  )
  op.create_index(
    op.f("ix_user_usage_tracking_user_id"),
    "user_usage_tracking",
    ["user_id"],
    unique=False,
  )
  op.create_table(
    "graph_credit_transactions",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("graph_credits_id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("transaction_type", sa.String(), nullable=False),
    sa.Column("amount", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column("description", sa.String(length=500), nullable=False),
    sa.Column("idempotency_key", sa.String(length=255), nullable=True),
    sa.Column("request_id", sa.String(length=255), nullable=True),
    sa.Column("operation_id", sa.String(length=255), nullable=True),
    sa.Column("user_id", sa.String(), nullable=True),
    sa.Column("metadata", sa.Text(), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(
      ["graph_credits_id"],
      ["graph_credits.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    "idx_credit_transactions_created",
    "graph_credit_transactions",
    ["created_at"],
    unique=False,
  )
  op.create_index(
    "idx_credit_transactions_dedup",
    "graph_credit_transactions",
    ["graph_credits_id", "transaction_type", "amount", "created_at", "operation_id"],
    unique=False,
  )
  op.create_index(
    "idx_credit_transactions_graph_credits",
    "graph_credit_transactions",
    ["graph_credits_id"],
    unique=False,
  )
  op.create_index(
    "idx_credit_transactions_graph_id",
    "graph_credit_transactions",
    ["graph_id"],
    unique=False,
  )
  op.create_index(
    "idx_credit_transactions_idempotency",
    "graph_credit_transactions",
    ["idempotency_key"],
    unique=True,
  )
  op.create_index(
    "idx_credit_transactions_operation_id",
    "graph_credit_transactions",
    ["operation_id"],
    unique=False,
  )
  op.create_index(
    "idx_credit_transactions_type",
    "graph_credit_transactions",
    ["transaction_type"],
    unique=False,
  )
  op.create_index(
    "idx_credit_transactions_user_id",
    "graph_credit_transactions",
    ["user_id"],
    unique=False,
  )
  op.create_table(
    "user_repository_credits",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("user_repository_id", sa.String(), nullable=False),
    sa.Column("current_balance", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column("monthly_allocation", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column(
      "credits_consumed_this_month", sa.Numeric(precision=10, scale=2), nullable=False
    ),
    sa.Column("last_consumption_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("last_allocation_date", sa.DateTime(timezone=True), nullable=True),
    sa.Column("next_allocation_date", sa.DateTime(timezone=True), nullable=True),
    sa.Column("allows_rollover", sa.Boolean(), nullable=False),
    sa.Column("max_rollover_credits", sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column("rollover_credits", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column("is_active", sa.Boolean(), nullable=False),
    sa.Column("suspended_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("suspension_reason", sa.String(), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(
      ["user_repository_id"],
      ["user_repository.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
    sa.UniqueConstraint("user_repository_id"),
  )
  op.create_index(
    "idx_user_repo_credits_access",
    "user_repository_credits",
    ["user_repository_id"],
    unique=False,
  )
  op.create_index(
    "idx_user_repo_credits_allocation",
    "user_repository_credits",
    ["next_allocation_date", "is_active"],
    unique=False,
  )
  op.create_index(
    "idx_user_repo_credits_balance",
    "user_repository_credits",
    ["current_balance"],
    unique=False,
  )
  op.create_table(
    "user_repository_credit_transactions",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("credit_pool_id", sa.String(), nullable=False),
    sa.Column("transaction_type", sa.String(), nullable=False),
    sa.Column("amount", sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column("description", sa.String(length=500), nullable=False),
    sa.Column("metadata", sa.Text(), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(
      ["credit_pool_id"],
      ["user_repository_credits.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    "idx_user_repo_credit_trans_created",
    "user_repository_credit_transactions",
    ["created_at"],
    unique=False,
  )
  op.create_index(
    "idx_user_repo_credit_trans_pool",
    "user_repository_credit_transactions",
    ["credit_pool_id"],
    unique=False,
  )
  op.create_index(
    "idx_user_repo_credit_trans_type",
    "user_repository_credit_transactions",
    ["transaction_type"],
    unique=False,
  )
  # ### end Alembic commands ###


def downgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.drop_index(
    "idx_user_repo_credit_trans_type", table_name="user_repository_credit_transactions"
  )
  op.drop_index(
    "idx_user_repo_credit_trans_pool", table_name="user_repository_credit_transactions"
  )
  op.drop_index(
    "idx_user_repo_credit_trans_created",
    table_name="user_repository_credit_transactions",
  )
  op.drop_table("user_repository_credit_transactions")
  op.drop_index("idx_user_repo_credits_balance", table_name="user_repository_credits")
  op.drop_index(
    "idx_user_repo_credits_allocation", table_name="user_repository_credits"
  )
  op.drop_index("idx_user_repo_credits_access", table_name="user_repository_credits")
  op.drop_table("user_repository_credits")
  op.drop_index(
    "idx_credit_transactions_user_id", table_name="graph_credit_transactions"
  )
  op.drop_index("idx_credit_transactions_type", table_name="graph_credit_transactions")
  op.drop_index(
    "idx_credit_transactions_operation_id", table_name="graph_credit_transactions"
  )
  op.drop_index(
    "idx_credit_transactions_idempotency", table_name="graph_credit_transactions"
  )
  op.drop_index(
    "idx_credit_transactions_graph_id", table_name="graph_credit_transactions"
  )
  op.drop_index(
    "idx_credit_transactions_graph_credits", table_name="graph_credit_transactions"
  )
  op.drop_index("idx_credit_transactions_dedup", table_name="graph_credit_transactions")
  op.drop_index(
    "idx_credit_transactions_created", table_name="graph_credit_transactions"
  )
  op.drop_table("graph_credit_transactions")
  op.drop_index(
    op.f("ix_user_usage_tracking_user_id"), table_name="user_usage_tracking"
  )
  op.drop_index(
    op.f("ix_user_usage_tracking_usage_type"), table_name="user_usage_tracking"
  )
  op.drop_index(
    op.f("ix_user_usage_tracking_occurred_at"), table_name="user_usage_tracking"
  )
  op.drop_index("idx_user_usage_type_time", table_name="user_usage_tracking")
  op.drop_index("idx_usage_type_time", table_name="user_usage_tracking")
  op.drop_table("user_usage_tracking")
  op.drop_index("idx_user_shared_repo_user_active", table_name="user_repository")
  op.drop_index("idx_user_shared_repo_type_level", table_name="user_repository")
  op.drop_index("idx_user_shared_repo_plan", table_name="user_repository")
  op.drop_index("idx_user_shared_repo_expires", table_name="user_repository")
  op.drop_index("idx_user_shared_repo_billing", table_name="user_repository")
  op.drop_table("user_repository")
  op.drop_table("user_limits")
  op.drop_index(op.f("ix_user_graphs_user_id"), table_name="user_graphs")
  op.drop_index(op.f("ix_user_graphs_graph_id"), table_name="user_graphs")
  op.drop_index("idx_user_graphs_user_selected", table_name="user_graphs")
  op.drop_index("idx_user_graphs_user_graph_id", table_name="user_graphs")
  op.drop_table("user_graphs")
  op.drop_index(op.f("ix_user_api_keys_user_id"), table_name="user_api_keys")
  op.drop_index(op.f("ix_user_api_keys_prefix"), table_name="user_api_keys")
  op.drop_index(op.f("ix_user_api_keys_key_hash"), table_name="user_api_keys")
  op.drop_index("idx_user_api_keys_prefix_active", table_name="user_api_keys")
  op.drop_index("idx_user_api_keys_last_used", table_name="user_api_keys")
  op.drop_index("idx_user_api_keys_hash_active", table_name="user_api_keys")
  op.drop_table("user_api_keys")
  op.drop_table("graph_subscriptions")
  op.drop_index("idx_graph_credits_user_id", table_name="graph_credits")
  op.drop_index("idx_graph_credits_storage_monitoring", table_name="graph_credits")
  op.drop_index("idx_graph_credits_graph_id", table_name="graph_credits")
  op.drop_index("idx_graph_credits_billing_admin", table_name="graph_credits")
  op.drop_table("graph_credits")
  op.drop_index(op.f("ix_graph_backups_status"), table_name="graph_backups")
  op.drop_index(op.f("ix_graph_backups_graph_id"), table_name="graph_backups")
  op.drop_index(op.f("ix_graph_backups_database_name"), table_name="graph_backups")
  op.drop_index(op.f("ix_graph_backups_created_by_user_id"), table_name="graph_backups")
  op.drop_table("graph_backups")
  op.drop_index(op.f("ix_users_email"), table_name="users")
  op.drop_table("users")
  op.drop_index(op.f("ix_graphs_graph_instance_id"), table_name="graphs")
  op.drop_index("idx_graphs_type", table_name="graphs")
  op.drop_index("idx_graphs_tier", table_name="graphs")
  op.drop_index(
    "idx_graphs_schema_extensions", table_name="graphs", postgresql_using="gin"
  )
  op.drop_index("idx_graphs_instance", table_name="graphs")
  op.drop_table("graphs")
  op.drop_index(
    op.f("ix_graph_usage_tracking_user_id"), table_name="graph_usage_tracking"
  )
  op.drop_index(
    op.f("ix_graph_usage_tracking_recorded_at"), table_name="graph_usage_tracking"
  )
  op.drop_index(
    op.f("ix_graph_usage_tracking_graph_tier"), table_name="graph_usage_tracking"
  )
  op.drop_index(
    op.f("ix_graph_usage_tracking_graph_id"), table_name="graph_usage_tracking"
  )
  op.drop_index(
    op.f("ix_graph_usage_tracking_event_type"), table_name="graph_usage_tracking"
  )
  op.drop_index(
    op.f("ix_graph_usage_tracking_billing_year"), table_name="graph_usage_tracking"
  )
  op.drop_index(
    op.f("ix_graph_usage_tracking_billing_month"), table_name="graph_usage_tracking"
  )
  op.drop_index(
    op.f("ix_graph_usage_tracking_billing_day"), table_name="graph_usage_tracking"
  )
  op.drop_index("idx_user_graph_time", table_name="graph_usage_tracking")
  op.drop_index("idx_storage_billing", table_name="graph_usage_tracking")
  op.drop_index("idx_performance_analysis", table_name="graph_usage_tracking")
  op.drop_index("idx_graph_tier_time", table_name="graph_usage_tracking")
  op.drop_index("idx_event_type_time", table_name="graph_usage_tracking")
  op.drop_index("idx_credits_consumed", table_name="graph_usage_tracking")
  op.drop_index("idx_billing_period", table_name="graph_usage_tracking")
  op.drop_table("graph_usage_tracking")
  op.drop_index(
    op.f("ix_connection_credentials_user_id"), table_name="connection_credentials"
  )
  op.drop_index(
    op.f("ix_connection_credentials_provider"), table_name="connection_credentials"
  )
  op.drop_index(
    op.f("ix_connection_credentials_connection_id"), table_name="connection_credentials"
  )
  op.drop_table("connection_credentials")
  # ### end Alembic commands ###
