package com.fs.common.constant;

public class LiveKeysConstant {

    public static final String USER_VISIT_KEY = "live:user:visit:";  // 用户访问标识用于判断是否是首次访问
    public static final String UNIQUE_VISITORS_KEY = "live:unique:visitors:";  //访客数
    public static final String UNIQUE_VIEWERS_KEY = "live:unique:viewers:";  //累计观看人数
    public static final String PAGE_VIEWS_KEY = "live:page:views:";  //浏览量
    public static final String TOTAL_VIEWS_KEY = "live:total:views:";  //累计观看人次
    public static final String MAX_ONLINE_USERS_KEY = "live:max:online:"; //最大在线人数
    public static final String ONLINE_USERS_KEY = "live:online:users:";  //当前在线人数
    public static final String ONLINE_USERS_SET_KEY = "live:online:users:set:";  //在线用户Set（用于统计最大同时在线人数）


    public static final String LIVE_HOME_PAGE_LIST = "live:homePage:list"; //直播列表数据
    public static final Integer LIVE_HOME_PAGE_LIST_EXPIRE = 300; //首页缓存过期时间
    public static final String LIVE_WATCH_USERS = "live:watch:users:%s"; //在线人数
    public static final String LIVE_COUPON_NUM = "live:coupon:num:%s"; //直播间优惠券数量
    /** 优惠券余量缓存 key 索引（勿用 KEYS live:coupon:num:*） */
    public static final String LIVE_COUPON_NUM_INDEX = "live:coupon:num:index";
    public static final String LIVE_COUPON_NUM_PREFIX = "live:coupon:num:";

    public static String liveCouponNumKey(Long couponIssueId) {
        return String.format(LIVE_COUPON_NUM, couponIssueId);
    }

    public static Long parseCouponIssueIdFromKey(String key) {
        if (key == null || !key.startsWith(LIVE_COUPON_NUM_PREFIX)) {
            return null;
        }
        return Long.parseLong(key.substring(LIVE_COUPON_NUM_PREFIX.length()));
    }

    public static final String LIVE_HOME_PAGE_DETAIL = "live:detail:%s"; //直播间详情
    public static final Integer LIVE_HOME_PAGE_DETAIL_EXPIRE = 300; //直播间详情过期时间

    public static final String LIVE_HOME_PAGE_CONFIG = "live:config:%s:%s"; //直播间配置信息
    public static final Integer LIVE_HOME_PAGE_CONFIG_EXPIRE = 300; //直播间配置信息过期时间
    public static final String LIVE_HOME_PAGE_CONFIG_RED = "live:config:%s:red:%s"; //积分记录
    public static final String LIVE_HOME_PAGE_CONFIG_COUPON = "live:config:%s:coupon:%s"; //优惠券记录
    public static final String LIVE_HOME_PAGE_CONFIG_DRAW = "live:config:%s:draw:%s"; //抽奖记录
    public static final String TOP_MSG = "topMsg"; //抽奖记录

    public static final String LIVE_FLAG_CACHE = "live:flag:%s"; //直播间直播/回放状态缓存
    public static final Integer LIVE_FLAG_CACHE_EXPIRE = 60; //直播间状态缓存过期时间（秒）

    public static final String LIVE_DATA_CACHE = "live:data:%s"; //直播间数据缓存
    public static final Integer LIVE_DATA_CACHE_EXPIRE = 300; //直播间数据缓存过期时间（秒）

    public static final String PRODUCT_DETAIL_CACHE = "product:detail:%s"; //商品详情缓存
    public static final Integer PRODUCT_DETAIL_CACHE_EXPIRE = 300; //商品详情缓存过期时间（秒）

    public static final String LIVE_TAG_MARK_CACHE = "live:tag:mark:%s"; //直播间打标签缓存，存储直播间ID、开始时间和视频时长
    /** 打标签缓存 key 索引（勿用 KEYS live:tag:mark:*） */
    public static final String LIVE_TAG_MARK_INDEX = "live:tag:mark:index";

    public static String liveTagMarkKey(Long liveId) {
        return String.format(LIVE_TAG_MARK_CACHE, liveId);
    }
    //记录用户观看直播间信息 直播间id、用户id、外部联系人id、qwUserId
    public static final String LIVE_USER_WATCH_LOG_CACHE = "live:user:watch:log:%s:%s:%s:%s";
    /** 用户观看日志活跃时间缓存索引（勿用 KEYS live:user:watch:log:*） */
    public static final String LIVE_USER_WATCH_LOG_INDEX = "live:user:watch:log:index";

    public static String liveUserWatchLogKey(Long liveId, Long userId, Long externalContactId, Long qwUserId) {
        return String.format(LIVE_USER_WATCH_LOG_CACHE, liveId, userId, externalContactId, qwUserId);
    }

    /** 直播评论飘屏/置顶全局配置缓存（单条） */
    public static final String LIVE_COMMENT_FEATURE_CONFIG_ROW = "live:comment:feature:config:row";
    public static final int LIVE_COMMENT_FEATURE_CONFIG_EXPIRE_SEC = 300;
    /** 飘屏冷却 liveId userId */
    public static final String LIVE_FLOAT_COOLDOWN = "live:float:cooldown:%s:%s";

    // ========== 直播下单动效提示 ==========
    /** 订单动效提示全局配置缓存（单条） */
    public static final String LIVE_ORDER_TIP_CONFIG_ROW = "live:order:tip:config:row";
    public static final int LIVE_ORDER_TIP_CONFIG_EXPIRE_SEC = 300;
    /** 订单动效滑动窗口（ZSet），记录最近 N 秒内已推送的时间戳，用于频率控制 %s=liveId */
    public static final String LIVE_ORDER_TIP_WINDOW = "live:order:tip:win:%s";
    /** 假订单填充节流：上次生成假订单时间 %s=liveId */
    public static final String LIVE_ORDER_TIP_FAKE_LAST = "live:order:tip:fake:last:%s";
    /** 假订单 scheduler 分布式锁（多节点防重复），%s=bucket */
    public static final String LIVE_ORDER_TIP_FAKE_LOCK = "live:order:tip:fake:lock:%s";
    /** 真实订单去重（防止同一订单多次推送），%s=orderId */
    public static final String LIVE_ORDER_TIP_REAL_DEDUP = "live:order:tip:real:dedup:%s";

    // ========== 直播延时任务 ZSet（定时扫描，勿用 KEYS） ==========
    public static final String LIVE_AUTO_TASK_PREFIX = "live:auto_task:";
    public static final String LIVE_LOTTERY_TASK_PREFIX = "live:lottery_task:";
    public static final String LIVE_RED_TASK_PREFIX = "live:red_task:";

    /** 活跃 ZSet key 索引，成员为完整 ZSet key，如 live:auto_task:123 */
    public static final String LIVE_AUTO_TASK_INDEX = "live:auto_task:index";
    public static final String LIVE_LOTTERY_TASK_INDEX = "live:lottery_task:index";
    public static final String LIVE_RED_TASK_INDEX = "live:red_task:index";

    public static String liveAutoTaskKey(Long liveId) {
        return LIVE_AUTO_TASK_PREFIX + liveId;
    }

    public static String liveLotteryTaskKey(Long liveId) {
        return LIVE_LOTTERY_TASK_PREFIX + liveId;
    }

    public static String liveRedTaskKey(Long liveId) {
        return LIVE_RED_TASK_PREFIX + liveId;
    }

    /** 从延时任务 ZSet key 解析直播间 ID */
    public static Long parseLiveIdFromTaskZSetKey(String zsetKey) {
        if (zsetKey == null || zsetKey.isEmpty()) {
            return null;
        }
        int idx = zsetKey.lastIndexOf(':');
        if (idx < 0 || idx >= zsetKey.length() - 1) {
            return null;
        }
        return Long.parseLong(zsetKey.substring(idx + 1));
    }

}
