-- CreateTable
CREATE TABLE "wallet_accounts" (
    "wallet_id" BIGSERIAL NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "usdt_available" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "usdt_frozen" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "dst_available" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "dst_frozen" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "bnb_available" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "bnb_frozen" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "og_available" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "og_frozen" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "rwad_available" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "rwad_frozen" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "pending_usdt" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "pending_hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "pending_expire_at" TIMESTAMP(3),
    "settleable_usdt" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "settleable_hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "settled_total_usdt" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "settled_total_hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "expired_total_usdt" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "expired_total_hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "wallet_accounts_pkey" PRIMARY KEY ("wallet_id")
);

-- CreateTable
CREATE TABLE "wallet_ledger_entries" (
    "entry_id" BIGSERIAL NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "entry_type" VARCHAR(50) NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "asset_type" VARCHAR(20) NOT NULL,
    "balance_after" DECIMAL(20,8),
    "ref_order_id" VARCHAR(100),
    "ref_tx_hash" VARCHAR(100),
    "memo" TEXT,
    "payload_json" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "wallet_ledger_entries_pkey" PRIMARY KEY ("entry_id")
);

-- CreateTable
CREATE TABLE "deposit_orders" (
    "order_id" BIGSERIAL NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "tx_hash" VARCHAR(100) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "confirmed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "deposit_orders_pkey" PRIMARY KEY ("order_id")
);

-- CreateTable
CREATE TABLE "settlement_orders" (
    "order_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "usdt_amount" DECIMAL(20,8) NOT NULL,
    "settle_currency" VARCHAR(10) NOT NULL,
    "swap_tx_hash" VARCHAR(100),
    "received_amount" DECIMAL(20,8),
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "settled_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "settlement_orders_pkey" PRIMARY KEY ("order_id")
);

-- CreateTable
CREATE TABLE "withdrawal_orders" (
    "order_id" BIGSERIAL NOT NULL,
    "order_no" VARCHAR(50) NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "fee" DECIMAL(20,8) NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "to_address" VARCHAR(100) NOT NULL,
    "tx_hash" VARCHAR(100),
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "error_message" VARCHAR(500),
    "frozen_at" TIMESTAMP(3),
    "broadcasted_at" TIMESTAMP(3),
    "confirmed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "withdrawal_orders_pkey" PRIMARY KEY ("order_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "wallet_accounts_account_sequence_key" ON "wallet_accounts"("account_sequence");

-- CreateIndex
CREATE UNIQUE INDEX "wallet_accounts_user_id_key" ON "wallet_accounts"("user_id");

-- CreateIndex
CREATE INDEX "wallet_accounts_user_id_idx" ON "wallet_accounts"("user_id");

-- CreateIndex
CREATE INDEX "wallet_accounts_usdt_available_idx" ON "wallet_accounts"("usdt_available" DESC);

-- CreateIndex
CREATE INDEX "wallet_accounts_hashpower_idx" ON "wallet_accounts"("hashpower" DESC);

-- CreateIndex
CREATE INDEX "wallet_accounts_status_idx" ON "wallet_accounts"("status");

-- CreateIndex
CREATE INDEX "wallet_ledger_entries_account_sequence_created_at_idx" ON "wallet_ledger_entries"("account_sequence", "created_at" DESC);

-- CreateIndex
CREATE INDEX "wallet_ledger_entries_user_id_created_at_idx" ON "wallet_ledger_entries"("user_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "wallet_ledger_entries_entry_type_idx" ON "wallet_ledger_entries"("entry_type");

-- CreateIndex
CREATE INDEX "wallet_ledger_entries_asset_type_idx" ON "wallet_ledger_entries"("asset_type");

-- CreateIndex
CREATE INDEX "wallet_ledger_entries_ref_order_id_idx" ON "wallet_ledger_entries"("ref_order_id");

-- CreateIndex
CREATE INDEX "wallet_ledger_entries_ref_tx_hash_idx" ON "wallet_ledger_entries"("ref_tx_hash");

-- CreateIndex
CREATE INDEX "wallet_ledger_entries_created_at_idx" ON "wallet_ledger_entries"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "deposit_orders_tx_hash_key" ON "deposit_orders"("tx_hash");

-- CreateIndex
CREATE INDEX "deposit_orders_account_sequence_idx" ON "deposit_orders"("account_sequence");

-- CreateIndex
CREATE INDEX "deposit_orders_user_id_idx" ON "deposit_orders"("user_id");

-- CreateIndex
CREATE INDEX "deposit_orders_tx_hash_idx" ON "deposit_orders"("tx_hash");

-- CreateIndex
CREATE INDEX "deposit_orders_status_idx" ON "deposit_orders"("status");

-- CreateIndex
CREATE INDEX "deposit_orders_chain_type_idx" ON "deposit_orders"("chain_type");

-- CreateIndex
CREATE INDEX "settlement_orders_user_id_idx" ON "settlement_orders"("user_id");

-- CreateIndex
CREATE INDEX "settlement_orders_status_idx" ON "settlement_orders"("status");

-- CreateIndex
CREATE INDEX "settlement_orders_settle_currency_idx" ON "settlement_orders"("settle_currency");

-- CreateIndex
CREATE INDEX "settlement_orders_created_at_idx" ON "settlement_orders"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "withdrawal_orders_order_no_key" ON "withdrawal_orders"("order_no");

-- CreateIndex
CREATE INDEX "withdrawal_orders_account_sequence_idx" ON "withdrawal_orders"("account_sequence");

-- CreateIndex
CREATE INDEX "withdrawal_orders_user_id_idx" ON "withdrawal_orders"("user_id");

-- CreateIndex
CREATE INDEX "withdrawal_orders_status_idx" ON "withdrawal_orders"("status");

-- CreateIndex
CREATE INDEX "withdrawal_orders_chain_type_idx" ON "withdrawal_orders"("chain_type");

-- CreateIndex
CREATE INDEX "withdrawal_orders_tx_hash_idx" ON "withdrawal_orders"("tx_hash");

-- CreateIndex
CREATE INDEX "withdrawal_orders_created_at_idx" ON "withdrawal_orders"("created_at");
