-- CreateEnum
CREATE TYPE "SystemAccountType" AS ENUM ('HEADQUARTERS', 'OPERATION', 'PROVINCE', 'CITY', 'FEE', 'HOT_WALLET', 'COLD_WALLET');

-- CreateEnum
CREATE TYPE "PoolAccountType" AS ENUM ('SHARE_POOL', 'BLACK_HOLE_POOL', 'CIRCULATION_POOL');

-- CreateEnum
CREATE TYPE "UserWalletType" AS ENUM ('CONTRIBUTION', 'TOKEN_STORAGE', 'GREEN_POINTS');

-- CreateEnum
CREATE TYPE "AssetType" AS ENUM ('SHARE', 'USDT', 'GREEN_POINT', 'CONTRIBUTION');

-- CreateEnum
CREATE TYPE "TransactionType" AS ENUM ('MINING_REWARD', 'MINING_DISTRIBUTE', 'TRANSFER_IN', 'TRANSFER_OUT', 'INTERNAL_TRANSFER', 'TRADE_BUY', 'TRADE_SELL', 'WITHDRAW', 'DEPOSIT', 'BURN', 'FREEZE', 'UNFREEZE', 'FEE_COLLECT', 'FEE_DISTRIBUTE', 'POOL_INJECT', 'POOL_EXTRACT', 'ADJUSTMENT', 'INITIAL_INJECT');

-- CreateEnum
CREATE TYPE "CounterpartyType" AS ENUM ('USER', 'SYSTEM_ACCOUNT', 'POOL', 'BLOCKCHAIN', 'EXTERNAL');

-- CreateEnum
CREATE TYPE "WithdrawStatus" AS ENUM ('PENDING', 'PROCESSING', 'CONFIRMING', 'COMPLETED', 'FAILED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED');

-- CreateTable
CREATE TABLE "provinces" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "provinces_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "cities" (
    "id" TEXT NOT NULL,
    "province_id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "cities_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user_region_mappings" (
    "id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "city_id" TEXT NOT NULL,
    "assigned_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "assigned_by" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "user_region_mappings_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "system_accounts" (
    "id" TEXT NOT NULL,
    "account_type" "SystemAccountType" NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "province_id" TEXT,
    "city_id" TEXT,
    "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "usdt_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "green_point_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_share" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_usdt" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "blockchain_address" TEXT,
    "description" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "system_accounts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "system_account_transactions" (
    "id" TEXT NOT NULL,
    "system_account_id" TEXT NOT NULL,
    "transaction_type" "TransactionType" NOT NULL,
    "asset_type" "AssetType" NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "balance_before" DECIMAL(30,8) NOT NULL,
    "balance_after" DECIMAL(30,8) NOT NULL,
    "counterparty_type" "CounterpartyType",
    "counterparty_account_seq" TEXT,
    "counterparty_user_id" TEXT,
    "counterparty_system_id" TEXT,
    "counterparty_pool_type" "PoolAccountType",
    "counterparty_address" TEXT,
    "reference_id" TEXT,
    "reference_type" TEXT,
    "tx_hash" TEXT,
    "memo" TEXT,
    "metadata" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "system_account_transactions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "pool_accounts" (
    "id" TEXT NOT NULL,
    "pool_type" "PoolAccountType" NOT NULL,
    "name" TEXT NOT NULL,
    "balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "target_burn" DECIMAL(30,8),
    "remaining_burn" DECIMAL(30,8),
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "description" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "pool_accounts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "pool_account_transactions" (
    "id" TEXT NOT NULL,
    "pool_account_id" TEXT NOT NULL,
    "pool_type" "PoolAccountType" NOT NULL,
    "transaction_type" "TransactionType" NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "balance_before" DECIMAL(30,8) NOT NULL,
    "balance_after" DECIMAL(30,8) NOT NULL,
    "counterparty_type" "CounterpartyType",
    "counterparty_account_seq" TEXT,
    "counterparty_user_id" TEXT,
    "counterparty_system_id" TEXT,
    "counterparty_pool_type" "PoolAccountType",
    "counterparty_address" TEXT,
    "reference_id" TEXT,
    "reference_type" TEXT,
    "tx_hash" TEXT,
    "memo" TEXT,
    "metadata" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "pool_account_transactions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user_wallets" (
    "id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "wallet_type" "UserWalletType" NOT NULL,
    "balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "user_wallets_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user_wallet_transactions" (
    "id" TEXT NOT NULL,
    "user_wallet_id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "wallet_type" "UserWalletType" NOT NULL,
    "transaction_type" "TransactionType" NOT NULL,
    "asset_type" "AssetType" NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "balance_before" DECIMAL(30,8) NOT NULL,
    "balance_after" DECIMAL(30,8) NOT NULL,
    "counterparty_type" "CounterpartyType",
    "counterparty_account_seq" TEXT,
    "counterparty_user_id" TEXT,
    "counterparty_system_id" TEXT,
    "counterparty_pool_type" "PoolAccountType",
    "counterparty_address" TEXT,
    "reference_id" TEXT,
    "reference_type" TEXT,
    "tx_hash" TEXT,
    "memo" TEXT,
    "metadata" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "user_wallet_transactions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "withdraw_requests" (
    "id" TEXT NOT NULL,
    "request_no" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "asset_type" "AssetType" NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "fee" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "net_amount" DECIMAL(30,8) NOT NULL,
    "to_address" TEXT NOT NULL,
    "status" "WithdrawStatus" NOT NULL DEFAULT 'PENDING',
    "tx_hash" TEXT,
    "block_number" BIGINT,
    "confirmations" INTEGER NOT NULL DEFAULT 0,
    "error_message" TEXT,
    "approved_by" TEXT,
    "approved_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "completed_at" TIMESTAMP(3),

    CONSTRAINT "withdraw_requests_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "deposit_records" (
    "id" TEXT NOT NULL,
    "tx_hash" TEXT NOT NULL,
    "from_address" TEXT NOT NULL,
    "to_address" TEXT NOT NULL,
    "asset_type" "AssetType" NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "block_number" BIGINT NOT NULL,
    "confirmations" INTEGER NOT NULL DEFAULT 0,
    "matched_account_seq" TEXT,
    "is_processed" BOOLEAN NOT NULL DEFAULT false,
    "processed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "deposit_records_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "dex_swap_records" (
    "id" TEXT NOT NULL,
    "swap_no" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "from_asset" "AssetType" NOT NULL,
    "to_asset" "AssetType" NOT NULL,
    "from_amount" DECIMAL(30,8) NOT NULL,
    "to_amount" DECIMAL(30,8) NOT NULL,
    "exchange_rate" DECIMAL(30,18) NOT NULL,
    "slippage" DECIMAL(10,4) NOT NULL DEFAULT 0,
    "fee" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "status" "WithdrawStatus" NOT NULL DEFAULT 'PENDING',
    "tx_hash" TEXT,
    "block_number" BIGINT,
    "error_message" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "completed_at" TIMESTAMP(3),

    CONSTRAINT "dex_swap_records_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "blockchain_address_bindings" (
    "id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "kava_address" TEXT NOT NULL,
    "is_verified" BOOLEAN NOT NULL DEFAULT false,
    "verified_at" TIMESTAMP(3),
    "verification_tx_hash" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "blockchain_address_bindings_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "black_hole_contracts" (
    "id" TEXT NOT NULL,
    "contract_address" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "target_burn" DECIMAL(30,8) NOT NULL,
    "remaining_burn" DECIMAL(30,8) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "black_hole_contracts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "burn_to_black_hole_records" (
    "id" TEXT NOT NULL,
    "black_hole_id" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "source_type" "CounterpartyType" NOT NULL,
    "source_account_seq" TEXT,
    "source_user_id" TEXT,
    "source_pool_type" "PoolAccountType",
    "tx_hash" TEXT,
    "block_number" BIGINT,
    "memo" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "burn_to_black_hole_records_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "fee_configs" (
    "id" TEXT NOT NULL,
    "fee_type" TEXT NOT NULL,
    "fee_rate" DECIMAL(10,6) NOT NULL,
    "min_fee" DECIMAL(30,8) NOT NULL,
    "max_fee" DECIMAL(30,8),
    "headquarters_rate" DECIMAL(10,6) NOT NULL,
    "operation_rate" DECIMAL(10,6) NOT NULL,
    "province_rate" DECIMAL(10,6) NOT NULL,
    "city_rate" DECIMAL(10,6) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "description" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "fee_configs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "outbox_events" (
    "id" TEXT NOT NULL,
    "aggregate_type" TEXT NOT NULL,
    "aggregate_id" TEXT NOT NULL,
    "event_type" TEXT NOT NULL,
    "payload" JSONB NOT NULL,
    "topic" TEXT NOT NULL DEFAULT 'mining-wallet.events',
    "key" TEXT,
    "status" "OutboxStatus" NOT NULL DEFAULT 'PENDING',
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "max_retries" INTEGER NOT NULL DEFAULT 10,
    "last_error" TEXT,
    "published_at" TIMESTAMP(3),
    "next_retry_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "outbox_events_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "processed_events" (
    "id" TEXT NOT NULL,
    "event_id" TEXT NOT NULL,
    "event_type" TEXT NOT NULL,
    "source_service" TEXT NOT NULL,
    "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "processed_events_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "audit_logs" (
    "id" TEXT NOT NULL,
    "operator_id" TEXT NOT NULL,
    "operator_type" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "resource" TEXT NOT NULL,
    "resource_id" TEXT,
    "old_value" JSONB,
    "new_value" JSONB,
    "ip_address" TEXT,
    "user_agent" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "audit_logs_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "provinces_code_key" ON "provinces"("code");

-- CreateIndex
CREATE INDEX "provinces_code_idx" ON "provinces"("code");

-- CreateIndex
CREATE UNIQUE INDEX "cities_code_key" ON "cities"("code");

-- CreateIndex
CREATE INDEX "cities_province_id_idx" ON "cities"("province_id");

-- CreateIndex
CREATE INDEX "cities_code_idx" ON "cities"("code");

-- CreateIndex
CREATE UNIQUE INDEX "user_region_mappings_account_sequence_key" ON "user_region_mappings"("account_sequence");

-- CreateIndex
CREATE INDEX "user_region_mappings_city_id_idx" ON "user_region_mappings"("city_id");

-- CreateIndex
CREATE UNIQUE INDEX "system_accounts_code_key" ON "system_accounts"("code");

-- CreateIndex
CREATE INDEX "system_accounts_account_type_idx" ON "system_accounts"("account_type");

-- CreateIndex
CREATE INDEX "system_accounts_province_id_idx" ON "system_accounts"("province_id");

-- CreateIndex
CREATE INDEX "system_accounts_city_id_idx" ON "system_accounts"("city_id");

-- CreateIndex
CREATE UNIQUE INDEX "system_accounts_account_type_province_id_city_id_key" ON "system_accounts"("account_type", "province_id", "city_id");

-- CreateIndex
CREATE INDEX "system_account_transactions_system_account_id_created_at_idx" ON "system_account_transactions"("system_account_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "system_account_transactions_transaction_type_idx" ON "system_account_transactions"("transaction_type");

-- CreateIndex
CREATE INDEX "system_account_transactions_counterparty_account_seq_idx" ON "system_account_transactions"("counterparty_account_seq");

-- CreateIndex
CREATE INDEX "system_account_transactions_counterparty_user_id_idx" ON "system_account_transactions"("counterparty_user_id");

-- CreateIndex
CREATE INDEX "system_account_transactions_reference_id_idx" ON "system_account_transactions"("reference_id");

-- CreateIndex
CREATE INDEX "system_account_transactions_created_at_idx" ON "system_account_transactions"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "pool_accounts_pool_type_key" ON "pool_accounts"("pool_type");

-- CreateIndex
CREATE INDEX "pool_accounts_pool_type_idx" ON "pool_accounts"("pool_type");

-- CreateIndex
CREATE INDEX "pool_account_transactions_pool_account_id_created_at_idx" ON "pool_account_transactions"("pool_account_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "pool_account_transactions_pool_type_transaction_type_idx" ON "pool_account_transactions"("pool_type", "transaction_type");

-- CreateIndex
CREATE INDEX "pool_account_transactions_counterparty_account_seq_idx" ON "pool_account_transactions"("counterparty_account_seq");

-- CreateIndex
CREATE INDEX "pool_account_transactions_counterparty_user_id_idx" ON "pool_account_transactions"("counterparty_user_id");

-- CreateIndex
CREATE INDEX "pool_account_transactions_reference_id_idx" ON "pool_account_transactions"("reference_id");

-- CreateIndex
CREATE INDEX "pool_account_transactions_created_at_idx" ON "pool_account_transactions"("created_at" DESC);

-- CreateIndex
CREATE INDEX "user_wallets_account_sequence_idx" ON "user_wallets"("account_sequence");

-- CreateIndex
CREATE INDEX "user_wallets_wallet_type_idx" ON "user_wallets"("wallet_type");

-- CreateIndex
CREATE UNIQUE INDEX "user_wallets_account_sequence_wallet_type_key" ON "user_wallets"("account_sequence", "wallet_type");

-- CreateIndex
CREATE INDEX "user_wallet_transactions_user_wallet_id_created_at_idx" ON "user_wallet_transactions"("user_wallet_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "user_wallet_transactions_account_sequence_wallet_type_idx" ON "user_wallet_transactions"("account_sequence", "wallet_type");

-- CreateIndex
CREATE INDEX "user_wallet_transactions_transaction_type_idx" ON "user_wallet_transactions"("transaction_type");

-- CreateIndex
CREATE INDEX "user_wallet_transactions_counterparty_account_seq_idx" ON "user_wallet_transactions"("counterparty_account_seq");

-- CreateIndex
CREATE INDEX "user_wallet_transactions_counterparty_user_id_idx" ON "user_wallet_transactions"("counterparty_user_id");

-- CreateIndex
CREATE INDEX "user_wallet_transactions_reference_id_idx" ON "user_wallet_transactions"("reference_id");

-- CreateIndex
CREATE INDEX "user_wallet_transactions_created_at_idx" ON "user_wallet_transactions"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "withdraw_requests_request_no_key" ON "withdraw_requests"("request_no");

-- CreateIndex
CREATE INDEX "withdraw_requests_account_sequence_idx" ON "withdraw_requests"("account_sequence");

-- CreateIndex
CREATE INDEX "withdraw_requests_status_idx" ON "withdraw_requests"("status");

-- CreateIndex
CREATE INDEX "withdraw_requests_tx_hash_idx" ON "withdraw_requests"("tx_hash");

-- CreateIndex
CREATE INDEX "withdraw_requests_created_at_idx" ON "withdraw_requests"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "deposit_records_tx_hash_key" ON "deposit_records"("tx_hash");

-- CreateIndex
CREATE INDEX "deposit_records_from_address_idx" ON "deposit_records"("from_address");

-- CreateIndex
CREATE INDEX "deposit_records_to_address_idx" ON "deposit_records"("to_address");

-- CreateIndex
CREATE INDEX "deposit_records_matched_account_seq_idx" ON "deposit_records"("matched_account_seq");

-- CreateIndex
CREATE INDEX "deposit_records_is_processed_idx" ON "deposit_records"("is_processed");

-- CreateIndex
CREATE INDEX "deposit_records_created_at_idx" ON "deposit_records"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "dex_swap_records_swap_no_key" ON "dex_swap_records"("swap_no");

-- CreateIndex
CREATE INDEX "dex_swap_records_account_sequence_idx" ON "dex_swap_records"("account_sequence");

-- CreateIndex
CREATE INDEX "dex_swap_records_status_idx" ON "dex_swap_records"("status");

-- CreateIndex
CREATE INDEX "dex_swap_records_tx_hash_idx" ON "dex_swap_records"("tx_hash");

-- CreateIndex
CREATE INDEX "dex_swap_records_created_at_idx" ON "dex_swap_records"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "blockchain_address_bindings_account_sequence_key" ON "blockchain_address_bindings"("account_sequence");

-- CreateIndex
CREATE UNIQUE INDEX "blockchain_address_bindings_kava_address_key" ON "blockchain_address_bindings"("kava_address");

-- CreateIndex
CREATE INDEX "blockchain_address_bindings_kava_address_idx" ON "blockchain_address_bindings"("kava_address");

-- CreateIndex
CREATE UNIQUE INDEX "black_hole_contracts_contract_address_key" ON "black_hole_contracts"("contract_address");

-- CreateIndex
CREATE INDEX "burn_to_black_hole_records_black_hole_id_idx" ON "burn_to_black_hole_records"("black_hole_id");

-- CreateIndex
CREATE INDEX "burn_to_black_hole_records_source_account_seq_idx" ON "burn_to_black_hole_records"("source_account_seq");

-- CreateIndex
CREATE INDEX "burn_to_black_hole_records_tx_hash_idx" ON "burn_to_black_hole_records"("tx_hash");

-- CreateIndex
CREATE INDEX "burn_to_black_hole_records_created_at_idx" ON "burn_to_black_hole_records"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "fee_configs_fee_type_key" ON "fee_configs"("fee_type");

-- CreateIndex
CREATE INDEX "outbox_events_status_idx" ON "outbox_events"("status");

-- CreateIndex
CREATE INDEX "outbox_events_next_retry_at_idx" ON "outbox_events"("next_retry_at");

-- CreateIndex
CREATE INDEX "outbox_events_created_at_idx" ON "outbox_events"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "processed_events_event_id_key" ON "processed_events"("event_id");

-- CreateIndex
CREATE INDEX "processed_events_source_service_idx" ON "processed_events"("source_service");

-- CreateIndex
CREATE INDEX "processed_events_processed_at_idx" ON "processed_events"("processed_at");

-- CreateIndex
CREATE INDEX "audit_logs_operator_id_idx" ON "audit_logs"("operator_id");

-- CreateIndex
CREATE INDEX "audit_logs_action_idx" ON "audit_logs"("action");

-- CreateIndex
CREATE INDEX "audit_logs_resource_idx" ON "audit_logs"("resource");

-- CreateIndex
CREATE INDEX "audit_logs_created_at_idx" ON "audit_logs"("created_at" DESC);

-- AddForeignKey
ALTER TABLE "cities" ADD CONSTRAINT "cities_province_id_fkey" FOREIGN KEY ("province_id") REFERENCES "provinces"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_region_mappings" ADD CONSTRAINT "user_region_mappings_city_id_fkey" FOREIGN KEY ("city_id") REFERENCES "cities"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "system_accounts" ADD CONSTRAINT "system_accounts_province_id_fkey" FOREIGN KEY ("province_id") REFERENCES "provinces"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "system_accounts" ADD CONSTRAINT "system_accounts_city_id_fkey" FOREIGN KEY ("city_id") REFERENCES "cities"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "system_account_transactions" ADD CONSTRAINT "system_account_transactions_system_account_id_fkey" FOREIGN KEY ("system_account_id") REFERENCES "system_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pool_account_transactions" ADD CONSTRAINT "pool_account_transactions_pool_account_id_fkey" FOREIGN KEY ("pool_account_id") REFERENCES "pool_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_wallet_transactions" ADD CONSTRAINT "user_wallet_transactions_user_wallet_id_fkey" FOREIGN KEY ("user_wallet_id") REFERENCES "user_wallets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "burn_to_black_hole_records" ADD CONSTRAINT "burn_to_black_hole_records_black_hole_id_fkey" FOREIGN KEY ("black_hole_id") REFERENCES "black_hole_contracts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

