-- CreateTable: reward_ledger_entries (聚合根1 - 行为表, append-only)
CREATE TABLE "reward_ledger_entries" (
    "entry_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "source_order_id" BIGINT NOT NULL,
    "source_user_id" BIGINT NOT NULL,
    "right_type" VARCHAR(50) NOT NULL,
    "usdt_amount" DECIMAL(20,8) NOT NULL,
    "hashpower_amount" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "reward_status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "created_at" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expire_at" TIMESTAMP(3),
    "claimed_at" TIMESTAMP(3),
    "settled_at" TIMESTAMP(3),
    "expired_at" TIMESTAMP(3),
    "memo" VARCHAR(500),

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

-- CreateTable: reward_summaries (聚合根2 - 状态表)
CREATE TABLE "reward_summaries" (
    "summary_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "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,
    "last_update_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "reward_summaries_pkey" PRIMARY KEY ("summary_id")
);

-- CreateTable: right_definitions (配置表)
CREATE TABLE "right_definitions" (
    "definition_id" BIGSERIAL NOT NULL,
    "right_type" VARCHAR(50) NOT NULL,
    "usdt_per_tree" DECIMAL(20,8) NOT NULL,
    "hashpower_percent" DECIMAL(5,2) NOT NULL DEFAULT 0,
    "payable_to" VARCHAR(50) NOT NULL,
    "rule_description" TEXT,
    "is_enabled" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "right_definitions_pkey" PRIMARY KEY ("definition_id")
);

-- CreateTable: settlement_records (行为表)
CREATE TABLE "settlement_records" (
    "settlement_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "usdt_amount" DECIMAL(20,8) NOT NULL,
    "hashpower_amount" DECIMAL(20,8) NOT NULL,
    "settle_currency" VARCHAR(10) NOT NULL,
    "received_amount" DECIMAL(20,8) NOT NULL,
    "swap_tx_hash" VARCHAR(100),
    "swap_rate" DECIMAL(20,8),
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),
    "reward_entry_ids" BIGINT[],

    CONSTRAINT "settlement_records_pkey" PRIMARY KEY ("settlement_id")
);

-- CreateTable: reward_events (行为表, append-only)
CREATE TABLE "reward_events" (
    "event_id" BIGSERIAL NOT NULL,
    "event_type" VARCHAR(50) NOT NULL,
    "aggregate_id" VARCHAR(100) NOT NULL,
    "aggregate_type" VARCHAR(50) NOT NULL,
    "event_data" JSONB NOT NULL,
    "user_id" BIGINT,
    "occurred_at" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "version" INTEGER NOT NULL DEFAULT 1,

    CONSTRAINT "reward_events_pkey" PRIMARY KEY ("event_id")
);

-- CreateIndex: reward_ledger_entries indexes
CREATE INDEX "idx_user_status" ON "reward_ledger_entries"("user_id", "reward_status");
CREATE INDEX "idx_user_created" ON "reward_ledger_entries"("user_id", "created_at" DESC);
CREATE INDEX "idx_source_order" ON "reward_ledger_entries"("source_order_id");
CREATE INDEX "idx_source_user" ON "reward_ledger_entries"("source_user_id");
CREATE INDEX "idx_right_type" ON "reward_ledger_entries"("right_type");
CREATE INDEX "idx_status" ON "reward_ledger_entries"("reward_status");
CREATE INDEX "idx_expire" ON "reward_ledger_entries"("expire_at");
CREATE INDEX "idx_created" ON "reward_ledger_entries"("created_at");

-- CreateIndex: reward_summaries indexes
CREATE UNIQUE INDEX "reward_summaries_user_id_key" ON "reward_summaries"("user_id");
CREATE INDEX "idx_summary_user" ON "reward_summaries"("user_id");
CREATE INDEX "idx_settleable_desc" ON "reward_summaries"("settleable_usdt" DESC);
CREATE INDEX "idx_pending_expire" ON "reward_summaries"("pending_expire_at");

-- CreateIndex: right_definitions indexes
CREATE UNIQUE INDEX "right_definitions_right_type_key" ON "right_definitions"("right_type");
CREATE INDEX "idx_def_right_type" ON "right_definitions"("right_type");
CREATE INDEX "idx_def_enabled" ON "right_definitions"("is_enabled");

-- CreateIndex: settlement_records indexes
CREATE INDEX "idx_settlement_user" ON "settlement_records"("user_id");
CREATE INDEX "idx_settlement_status" ON "settlement_records"("status");
CREATE INDEX "idx_settlement_created" ON "settlement_records"("created_at");

-- CreateIndex: reward_events indexes
CREATE INDEX "idx_reward_event_aggregate" ON "reward_events"("aggregate_type", "aggregate_id");
CREATE INDEX "idx_reward_event_type" ON "reward_events"("event_type");
CREATE INDEX "idx_reward_event_user" ON "reward_events"("user_id");
CREATE INDEX "idx_reward_event_occurred" ON "reward_events"("occurred_at");
