CREATE TABLE orders (
                        id BIGINT PRIMARY KEY,
                        consumer_id BIGINT NOT NULL,
                        consumer_name VARCHAR(100) NOT NULL,
                        player_id BIGINT NOT NULL,
                        player_name VARCHAR(100) NOT NULL,
                        shop_id BIGINT,
                        shop_name VARCHAR(200),
                        service_snapshot JSONB NOT NULL,
                        status VARCHAR(30) NOT NULL DEFAULT 'pending_payment',
                        total_price DECIMAL(10,2) NOT NULL,
                        note TEXT,
                        version INT NOT NULL DEFAULT 1,
                        timeout_job_id VARCHAR(100),
                        search_text TEXT GENERATED ALWAYS AS (
                            consumer_name || ' ' || player_name || ' ' || coalesce(shop_name, '') || ' ' || coalesce(note, '')
                            ) STORED,
                        created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                        accepted_at TIMESTAMPTZ,
                        closed_at TIMESTAMPTZ,
                        completed_at TIMESTAMPTZ,
                        cancelled_at TIMESTAMPTZ,
                        updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

                        CONSTRAINT chk_order_status CHECK (status IN (
                                                                      'pending_payment', 'pending_accept', 'in_progress',
                                                                      'pending_close', 'pending_review', 'disputed',
                                                                      'completed', 'cancelled'
                            )),
                        CONSTRAINT chk_price_positive CHECK (total_price > 0)
);

-- 基础索引
CREATE INDEX idx_orders_consumer ON orders(consumer_id, created_at DESC);
CREATE INDEX idx_orders_player ON orders(player_id, created_at DESC);
CREATE INDEX idx_orders_shop ON orders(shop_id, created_at DESC) WHERE shop_id IS NOT NULL;
CREATE INDEX idx_orders_status ON orders(status, created_at DESC);
CREATE INDEX idx_orders_timeout ON orders(timeout_job_id) WHERE timeout_job_id IS NOT NULL;

-- 三元组索引用于订单搜索
CREATE INDEX idx_orders_search_trgm ON orders USING gin(search_text gin_trgm_ops);

-- 复合索引优化多条件查询
CREATE INDEX idx_orders_consumer_status_created ON orders(consumer_id, status, created_at DESC);
CREATE INDEX idx_orders_player_status_created ON orders(player_id, status, created_at DESC);
CREATE INDEX idx_orders_shop_status_created ON orders(shop_id, status, created_at DESC)
    WHERE shop_id IS NOT NULL;

-- 状态+时间复合索引 (用于超时任务扫描)
CREATE INDEX idx_orders_status_timeout ON orders(status, created_at)
    WHERE status IN ('pending_accept', 'pending_close', 'pending_review');

-- JSONB 索引优化服务快照查询
CREATE INDEX idx_orders_service_snapshot ON orders USING gin(service_snapshot);

-- 价格区间索引
CREATE INDEX idx_orders_price ON orders(total_price) WHERE status = 'completed';

-- 时间范围索引 (用于统计)
CREATE INDEX idx_orders_completed_at ON orders(completed_at DESC) WHERE completed_at IS NOT NULL;

CREATE TRIGGER trigger_orders_updated_at
    BEFORE UPDATE ON orders
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();