-- =============================================================================
-- CDC Query Views Migration
-- 创建用于 Debezium CDC 数据同步的查询视图表
-- =============================================================================

-- -----------------------------------------------------------------------------
-- Wallet Query Views (钱包查询视图)
-- -----------------------------------------------------------------------------

-- 钱包账户查询视图
CREATE TABLE "wallet_account_query_view" (
    "wallet_id" BIGINT 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,
    "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',
    "has_planted" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE UNIQUE INDEX "wallet_account_query_view_account_sequence_key" ON "wallet_account_query_view"("account_sequence");
CREATE UNIQUE INDEX "wallet_account_query_view_user_id_key" ON "wallet_account_query_view"("user_id");
CREATE INDEX "wallet_account_query_view_account_sequence_idx" ON "wallet_account_query_view"("account_sequence");
CREATE INDEX "wallet_account_query_view_usdt_available_idx" ON "wallet_account_query_view"("usdt_available" DESC);
CREATE INDEX "wallet_account_query_view_hashpower_idx" ON "wallet_account_query_view"("hashpower" DESC);
CREATE INDEX "wallet_account_query_view_status_idx" ON "wallet_account_query_view"("status");

-- 提现订单查询视图
CREATE TABLE "withdrawal_order_query_view" (
    "order_id" BIGINT 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),
    "is_internal_transfer" BOOLEAN NOT NULL DEFAULT false,
    "to_account_sequence" VARCHAR(20),
    "to_user_id" BIGINT,
    "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,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE UNIQUE INDEX "withdrawal_order_query_view_order_no_key" ON "withdrawal_order_query_view"("order_no");
CREATE INDEX "withdrawal_order_query_view_account_sequence_idx" ON "withdrawal_order_query_view"("account_sequence");
CREATE INDEX "withdrawal_order_query_view_user_id_idx" ON "withdrawal_order_query_view"("user_id");
CREATE INDEX "withdrawal_order_query_view_status_idx" ON "withdrawal_order_query_view"("status");
CREATE INDEX "withdrawal_order_query_view_chain_type_idx" ON "withdrawal_order_query_view"("chain_type");
CREATE INDEX "withdrawal_order_query_view_tx_hash_idx" ON "withdrawal_order_query_view"("tx_hash");
CREATE INDEX "withdrawal_order_query_view_created_at_idx" ON "withdrawal_order_query_view"("created_at");

-- 法币提现订单查询视图
CREATE TABLE "fiat_withdrawal_order_query_view" (
    "order_id" BIGINT 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,
    "payment_method" VARCHAR(20) NOT NULL,
    "bank_name" VARCHAR(100),
    "bank_card_no_masked" VARCHAR(50),
    "card_holder_name" VARCHAR(100),
    "alipay_account_masked" VARCHAR(100),
    "wechat_account_masked" VARCHAR(100),
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "error_message" VARCHAR(500),
    "reviewed_by" VARCHAR(100),
    "reviewed_at" TIMESTAMP(3),
    "review_remark" VARCHAR(500),
    "paid_by" VARCHAR(100),
    "paid_at" TIMESTAMP(3),
    "frozen_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE UNIQUE INDEX "fiat_withdrawal_order_query_view_order_no_key" ON "fiat_withdrawal_order_query_view"("order_no");
CREATE INDEX "fiat_withdrawal_order_query_view_account_sequence_idx" ON "fiat_withdrawal_order_query_view"("account_sequence");
CREATE INDEX "fiat_withdrawal_order_query_view_user_id_idx" ON "fiat_withdrawal_order_query_view"("user_id");
CREATE INDEX "fiat_withdrawal_order_query_view_status_idx" ON "fiat_withdrawal_order_query_view"("status");
CREATE INDEX "fiat_withdrawal_order_query_view_payment_method_idx" ON "fiat_withdrawal_order_query_view"("payment_method");
CREATE INDEX "fiat_withdrawal_order_query_view_reviewed_by_idx" ON "fiat_withdrawal_order_query_view"("reviewed_by");
CREATE INDEX "fiat_withdrawal_order_query_view_created_at_idx" ON "fiat_withdrawal_order_query_view"("created_at");

-- -----------------------------------------------------------------------------
-- Planting Query Views (认种查询视图)
-- -----------------------------------------------------------------------------

-- 认种订单查询视图
CREATE TABLE "planting_order_query_view" (
    "order_id" BIGINT NOT NULL,
    "order_no" VARCHAR(50) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "tree_count" INTEGER NOT NULL,
    "total_amount" DECIMAL(20,8) NOT NULL,
    "selected_province" VARCHAR(10),
    "selected_city" VARCHAR(10),
    "status" VARCHAR(30) NOT NULL DEFAULT 'CREATED',
    "pool_injection_batch_id" BIGINT,
    "created_at" TIMESTAMP(3) NOT NULL,
    "paid_at" TIMESTAMP(3),
    "fund_allocated_at" TIMESTAMP(3),
    "mining_enabled_at" TIMESTAMP(3),
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE UNIQUE INDEX "planting_order_query_view_order_no_key" ON "planting_order_query_view"("order_no");
CREATE INDEX "planting_order_query_view_user_id_idx" ON "planting_order_query_view"("user_id");
CREATE INDEX "planting_order_query_view_account_sequence_idx" ON "planting_order_query_view"("account_sequence");
CREATE INDEX "planting_order_query_view_status_idx" ON "planting_order_query_view"("status");
CREATE INDEX "planting_order_query_view_province_city_idx" ON "planting_order_query_view"("selected_province", "selected_city");
CREATE INDEX "planting_order_query_view_created_at_idx" ON "planting_order_query_view"("created_at");
CREATE INDEX "planting_order_query_view_paid_at_idx" ON "planting_order_query_view"("paid_at");

-- 用户持仓查询视图
CREATE TABLE "planting_position_query_view" (
    "position_id" BIGINT NOT NULL,
    "user_id" BIGINT NOT NULL,
    "total_tree_count" INTEGER NOT NULL DEFAULT 0,
    "effective_tree_count" INTEGER NOT NULL DEFAULT 0,
    "pending_tree_count" INTEGER NOT NULL DEFAULT 0,
    "first_mining_start_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "planting_position_query_view_pkey" PRIMARY KEY ("position_id")
);

CREATE UNIQUE INDEX "planting_position_query_view_user_id_key" ON "planting_position_query_view"("user_id");
CREATE INDEX "planting_position_query_view_total_tree_count_idx" ON "planting_position_query_view"("total_tree_count");

-- 合同签署任务查询视图
CREATE TABLE "contract_signing_task_query_view" (
    "task_id" BIGINT NOT NULL,
    "order_no" VARCHAR(50) NOT NULL,
    "contract_no" VARCHAR(30) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "contract_version" VARCHAR(20) NOT NULL,
    "tree_count" INTEGER NOT NULL,
    "total_amount" DECIMAL(20,8) NOT NULL,
    "province_code" VARCHAR(10) NOT NULL,
    "province_name" VARCHAR(50) NOT NULL,
    "city_code" VARCHAR(10) NOT NULL,
    "city_name" VARCHAR(50) NOT NULL,
    "status" VARCHAR(30) NOT NULL DEFAULT 'PENDING',
    "expires_at" TIMESTAMP(3) NOT NULL,
    "signed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "contract_signing_task_query_view_pkey" PRIMARY KEY ("task_id")
);

CREATE UNIQUE INDEX "contract_signing_task_query_view_order_no_key" ON "contract_signing_task_query_view"("order_no");
CREATE UNIQUE INDEX "contract_signing_task_query_view_contract_no_key" ON "contract_signing_task_query_view"("contract_no");
CREATE INDEX "contract_signing_task_query_view_user_id_idx" ON "contract_signing_task_query_view"("user_id");
CREATE INDEX "contract_signing_task_query_view_status_idx" ON "contract_signing_task_query_view"("status");
CREATE INDEX "contract_signing_task_query_view_expires_at_idx" ON "contract_signing_task_query_view"("expires_at");

-- -----------------------------------------------------------------------------
-- Authorization Query Views (授权查询视图)
-- -----------------------------------------------------------------------------

-- 授权角色查询视图
CREATE TABLE "authorization_role_query_view" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "role_type" VARCHAR(30) NOT NULL,
    "region_code" TEXT NOT NULL,
    "region_name" TEXT NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "display_title" TEXT NOT NULL,
    "authorized_at" TIMESTAMP(3),
    "authorized_by" TEXT,
    "revoked_at" TIMESTAMP(3),
    "revoked_by" TEXT,
    "deleted_at" TIMESTAMP(3),
    "initial_target_tree_count" INTEGER NOT NULL,
    "monthly_target_type" VARCHAR(20) NOT NULL,
    "benefit_active" BOOLEAN NOT NULL DEFAULT false,
    "benefit_activated_at" TIMESTAMP(3),
    "last_assessment_month" TEXT,
    "monthly_trees_added" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "authorization_role_query_view_account_sequence_idx" ON "authorization_role_query_view"("account_sequence");
CREATE INDEX "authorization_role_query_view_user_id_idx" ON "authorization_role_query_view"("user_id");
CREATE INDEX "authorization_role_query_view_role_type_region_code_idx" ON "authorization_role_query_view"("role_type", "region_code");
CREATE INDEX "authorization_role_query_view_status_idx" ON "authorization_role_query_view"("status");
CREATE INDEX "authorization_role_query_view_deleted_at_idx" ON "authorization_role_query_view"("deleted_at");

-- 月度考核查询视图
CREATE TABLE "monthly_assessment_query_view" (
    "id" TEXT NOT NULL,
    "authorization_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "role_type" VARCHAR(30) NOT NULL,
    "region_code" TEXT NOT NULL,
    "assessment_month" TEXT NOT NULL,
    "month_index" INTEGER NOT NULL,
    "monthly_target" INTEGER NOT NULL,
    "cumulative_target" INTEGER NOT NULL,
    "monthly_completed" INTEGER NOT NULL DEFAULT 0,
    "cumulative_completed" INTEGER NOT NULL DEFAULT 0,
    "completed_at" TIMESTAMP(3),
    "result" VARCHAR(20) NOT NULL DEFAULT 'NOT_ASSESSED',
    "ranking_in_region" INTEGER,
    "is_first_place" BOOLEAN NOT NULL DEFAULT false,
    "is_bypassed" BOOLEAN NOT NULL DEFAULT false,
    "assessed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "monthly_assessment_query_view_account_month_idx" ON "monthly_assessment_query_view"("account_sequence", "assessment_month");
CREATE INDEX "monthly_assessment_query_view_user_month_idx" ON "monthly_assessment_query_view"("user_id", "assessment_month");
CREATE INDEX "monthly_assessment_query_view_role_region_month_idx" ON "monthly_assessment_query_view"("role_type", "region_code", "assessment_month");
CREATE INDEX "monthly_assessment_query_view_month_result_idx" ON "monthly_assessment_query_view"("assessment_month", "result");

-- 系统账户查询视图
CREATE TABLE "system_account_query_view" (
    "account_id" BIGINT NOT NULL,
    "account_type" VARCHAR(30) NOT NULL,
    "region_code" VARCHAR(10),
    "region_name" VARCHAR(50),
    "wallet_address" VARCHAR(42),
    "usdt_balance" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "total_received" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "total_transferred" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "system_account_query_view_pkey" PRIMARY KEY ("account_id")
);

CREATE INDEX "system_account_query_view_account_type_idx" ON "system_account_query_view"("account_type");
CREATE INDEX "system_account_query_view_wallet_address_idx" ON "system_account_query_view"("wallet_address");

-- -----------------------------------------------------------------------------
-- Ledger Views (分类账视图)
-- -----------------------------------------------------------------------------

-- 钱包流水分类账
CREATE TABLE "wallet_ledger_entry_view" (
    "entry_id" BIGINT 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,
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "wallet_ledger_entry_view_account_created_idx" ON "wallet_ledger_entry_view"("account_sequence", "created_at" DESC);
CREATE INDEX "wallet_ledger_entry_view_user_created_idx" ON "wallet_ledger_entry_view"("user_id", "created_at" DESC);
CREATE INDEX "wallet_ledger_entry_view_entry_type_idx" ON "wallet_ledger_entry_view"("entry_type");
CREATE INDEX "wallet_ledger_entry_view_asset_type_idx" ON "wallet_ledger_entry_view"("asset_type");
CREATE INDEX "wallet_ledger_entry_view_ref_order_id_idx" ON "wallet_ledger_entry_view"("ref_order_id");
CREATE INDEX "wallet_ledger_entry_view_ref_tx_hash_idx" ON "wallet_ledger_entry_view"("ref_tx_hash");
CREATE INDEX "wallet_ledger_entry_view_created_at_idx" ON "wallet_ledger_entry_view"("created_at");

-- 认种资金分配分类账
CREATE TABLE "fund_allocation_view" (
    "allocation_id" BIGINT NOT NULL,
    "order_id" BIGINT NOT NULL,
    "target_type" VARCHAR(50) NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "target_account_id" VARCHAR(100),
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "fund_allocation_view_pkey" PRIMARY KEY ("allocation_id")
);

CREATE INDEX "fund_allocation_view_order_id_idx" ON "fund_allocation_view"("order_id");
CREATE INDEX "fund_allocation_view_target_idx" ON "fund_allocation_view"("target_type", "target_account_id");
CREATE INDEX "fund_allocation_view_created_at_idx" ON "fund_allocation_view"("created_at");

-- 系统账户流水分类账
CREATE TABLE "system_account_ledger_view" (
    "ledger_id" BIGINT NOT NULL,
    "account_id" BIGINT NOT NULL,
    "entry_type" VARCHAR(30) NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "balance_after" DECIMAL(20,8) NOT NULL,
    "source_order_id" BIGINT,
    "source_reward_id" BIGINT,
    "tx_hash" VARCHAR(66),
    "memo" VARCHAR(500),
    "created_at" TIMESTAMP(3) NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "system_account_ledger_view_pkey" PRIMARY KEY ("ledger_id")
);

CREATE INDEX "system_account_ledger_view_account_created_idx" ON "system_account_ledger_view"("account_id", "created_at" DESC);
CREATE INDEX "system_account_ledger_view_source_order_idx" ON "system_account_ledger_view"("source_order_id");
CREATE INDEX "system_account_ledger_view_tx_hash_idx" ON "system_account_ledger_view"("tx_hash");
CREATE INDEX "system_account_ledger_view_created_at_idx" ON "system_account_ledger_view"("created_at");
