-- 为套餐价格回退与赠送策略补充价格状态、赠送标记和历史快照字段
-- 同时创建人工复核清单表，承接无法确认语义的历史 0 价数据

ALTER TABLE tb_package
    ADD COLUMN IF NOT EXISTS price_config_status INT NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS is_gift BOOLEAN NOT NULL DEFAULT FALSE;

ALTER TABLE tb_shop_package_allocation
    ADD COLUMN IF NOT EXISTS retail_price_config_status INT NOT NULL DEFAULT 0;

ALTER TABLE tb_order_item
    ADD COLUMN IF NOT EXISTS package_price_config_status INT NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS package_is_gift BOOLEAN NOT NULL DEFAULT FALSE;

ALTER TABLE tb_package_usage
    ADD COLUMN IF NOT EXISTS package_price_config_status INT NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS package_is_gift BOOLEAN NOT NULL DEFAULT FALSE;

COMMENT ON COLUMN tb_package.price_config_status IS '价格配置状态 0-未配置 1-赠送0价 2-已配置非0';
COMMENT ON COLUMN tb_package.is_gift IS '是否赠送套餐 true-赠送 false-普通可售';
COMMENT ON COLUMN tb_shop_package_allocation.retail_price_config_status IS '零售价配置状态 0-未配置 1-赠送0价 2-已配置非0';
COMMENT ON COLUMN tb_order_item.package_price_config_status IS '套餐价格配置状态快照 0-未配置 1-赠送0价 2-已配置非0';
COMMENT ON COLUMN tb_order_item.package_is_gift IS '套餐是否赠送快照 true-赠送 false-普通可售';
COMMENT ON COLUMN tb_package_usage.package_price_config_status IS '套餐价格配置状态快照 0-未配置 1-赠送0价 2-已配置非0';
COMMENT ON COLUMN tb_package_usage.package_is_gift IS '套餐是否赠送快照 true-赠送 false-普通可售';

CREATE TABLE IF NOT EXISTS tb_package_price_review_queue (
    id BIGSERIAL PRIMARY KEY,
    package_id BIGINT NOT NULL,
    package_name VARCHAR(255) NOT NULL DEFAULT '',
    raw_suggested_retail_price BIGINT NOT NULL DEFAULT 0,
    review_reason VARCHAR(255) NOT NULL DEFAULT '',
    review_status INT NOT NULL DEFAULT 0,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE tb_package_price_review_queue IS '套餐价格人工复核清单';
COMMENT ON COLUMN tb_package_price_review_queue.package_id IS '套餐ID';
COMMENT ON COLUMN tb_package_price_review_queue.package_name IS '套餐名称快照';
COMMENT ON COLUMN tb_package_price_review_queue.raw_suggested_retail_price IS '原始建议售价';
COMMENT ON COLUMN tb_package_price_review_queue.review_reason IS '复核原因';
COMMENT ON COLUMN tb_package_price_review_queue.review_status IS '复核状态 0-待处理 1-已处理';

-- 非 0 售价直接视为已配置非 0
UPDATE tb_package
SET price_config_status = 2,
    is_gift = FALSE
WHERE suggested_retail_price > 0;

-- 显式识别“后台创建订单且 0 元成交，且未进入代理分配”的赠送套餐
UPDATE tb_package p
SET price_config_status = 1,
    is_gift = TRUE
WHERE p.suggested_retail_price = 0
  AND EXISTS (
    SELECT 1
    FROM tb_order_item oi
    JOIN tb_order o ON o.id = oi.order_id
    WHERE oi.package_id = p.id
      AND oi.unit_price = 0
      AND o.source = 'admin'
      AND o.payment_status = 2
  )
  AND NOT EXISTS (
    SELECT 1
    FROM tb_shop_package_allocation spa
    WHERE spa.package_id = p.id
      AND spa.deleted_at IS NULL
      AND spa.allocator_shop_id > 0
  );

-- 其余历史 0 价统一回填为“未配置”，并进入人工复核清单
INSERT INTO tb_package_price_review_queue (package_id, package_name, raw_suggested_retail_price, review_reason)
SELECT p.id,
       p.package_name,
       p.suggested_retail_price,
       '历史 0 价无法确认是否为赠送，已回填为未配置'
FROM tb_package p
WHERE p.suggested_retail_price = 0
  AND p.price_config_status = 0
  AND NOT EXISTS (
    SELECT 1
    FROM tb_package_price_review_queue q
    WHERE q.package_id = p.id
  );

-- 分配记录：显式非 0 视为已配置，其余继承套餐层语义
UPDATE tb_shop_package_allocation spa
SET retail_price_config_status = CASE
    WHEN spa.retail_price > 0 THEN 2
    ELSE p.price_config_status
END
FROM tb_package p
WHERE p.id = spa.package_id;

-- 历史价格快照补齐价格状态和赠送语义
UPDATE tb_order_item oi
SET package_price_config_status = p.price_config_status,
    package_is_gift = p.is_gift
FROM tb_package p
WHERE p.id = oi.package_id;

UPDATE tb_package_usage pu
SET package_price_config_status = p.price_config_status,
    package_is_gift = p.is_gift
FROM tb_package p
WHERE p.id = pu.package_id;
