-- V8: 将mock资产插入展示表exhibitions (2026-05-17)
-- 为所有mock资产(asset_id: 10001-40037)创建展示记录
-- 结束时间 = 开始时间 + 1个月

BEGIN;

-- 获取当前时间戳(毫秒)作为开始时间
DO $$
DECLARE
    start_ts bigint := (EXTRACT(EPOCH FROM NOW()) * 1000)::bigint;
    expire_ts bigint := (EXTRACT(EPOCH FROM NOW()) * 1000)::bigint + (30::bigint * 24 * 60 * 60 * 1000); -- 加1个月
    slot_ids bigint[];
    slot_rec record;
    asset_rec record;
    i int := 0;
BEGIN
    -- 收集star_id=87的所有可用slot_id
    SELECT array_agg(slot_id ORDER BY slot_id) INTO slot_ids FROM booth_slots WHERE star_id = 87;

    -- 为每个mock资产创建展示记录
    FOR asset_rec IN SELECT id, owner_uid FROM assets WHERE id >= 10000 ORDER BY id LOOP
        -- 循环分配slot_id
        IF array_length(slot_ids, 1) > 0 THEN
            i := (i % array_length(slot_ids, 1)) + 1;

            -- 获取对应slot的host_profile_id
            SELECT host_profile_id INTO slot_rec FROM booth_slots WHERE slot_id = slot_ids[i];

            INSERT INTO exhibitions (asset_id, slot_id, host_profile_id, occupier_uid, occupier_star_id, start_time, expire_at, created_at, updated_at)
            VALUES (asset_rec.id, slot_ids[i], slot_rec.host_profile_id, asset_rec.owner_uid, 87, start_ts, expire_ts, start_ts, start_ts)
            ON CONFLICT (asset_id) WHERE deleted_at IS NULL DO NOTHING;
        END IF;
    END LOOP;
END $$;

-- 将asset_registry的display_status设为1
UPDATE asset_registry SET display_status = 1 WHERE id >= 10000;

COMMIT;