package com.fs.app.controller.live;

import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.his.domain.FsUser;
import com.fs.his.service.IFsUserService;
import com.fs.live.domain.LiveData;
import com.fs.live.domain.LiveUserBehaviorTrack;
import com.fs.live.service.ILiveDataService;
import com.fs.live.service.ILiveUserBehaviorTrackService;
import com.fs.live.service.ILiveWatchUserService;
import com.fs.live.util.LiveBehaviorTrackUtil;
import com.fs.live.vo.HisFsUserVO;
import com.fs.live.vo.LiveWatchUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/app/live/liveData")
public class LiveDataController extends AppBaseController {

    @Autowired
    private ILiveDataService liveDataService;

    @Autowired
    private ILiveWatchUserService liveWatchUserService;

    @Autowired
    IFsUserService fsUserService;

    @Autowired
    private ILiveUserBehaviorTrackService behaviorTrackService;
    /**
     * 查询直播数据列表
     * */
    @Login
    @GetMapping("/list")
    public List<LiveData> list(LiveData liveData) {
        return liveDataService.selectLiveDataList(liveData);
    }

    /**
     * 查询直播数据详情
     * */
    @Login
    @GetMapping("/get/{liveId}")
    public R getLiveData(@PathVariable("liveId") Long liveId) {
        return R.ok().put("liveData", liveDataService.selectLiveDataByLiveId(liveId));
    }

    /**
     * 点赞
     * */
    @Login
    @GetMapping("/like/{liveId}")
    @ApiIgnore("直播端点赞接口")
    public R like(@PathVariable("liveId") Long liveId) {
        Long userId = Long.parseLong(getUserId());
        
        // 埋点：记录点赞行为
        if (liveId != null && userId != null) {
            try {
                LiveUserBehaviorTrack track = LiveBehaviorTrackUtil.trackLike(userId, liveId);
                behaviorTrackService.asyncInsertLiveUserBehaviorTrack(track);
            } catch (Exception e) {
                log.error("[埋点] 记录点赞行为失败, liveId={}, userId={}",
                        liveId, userId, e);
            }
        }
        
        return liveDataService.updateLikeByLiveId(liveId, userId);
    }

    /**
     * 收藏
     * */
    @Login
    @GetMapping("/collect/{liveId}")
    public R collect(@PathVariable("liveId") Long liveId) {
        return R.ok(liveDataService.collect(liveId,  Long.parseLong(getUserId())));
    }

    /**
     * 关注
     * */
    @Login
    @GetMapping("/follow/{liveId}")
    public R follow(@PathVariable("liveId") Long liveId) {
        return R.ok(liveDataService.follow(liveId,  Long.parseLong(getUserId())));
    }

    /**
     * 收藏/取消收藏店铺
     * */
    @Login
    @PostMapping("/collectStore")
    public R collectStore(@RequestParam Long storeId) {
        return R.ok(liveDataService.collectStore(Long.parseLong(getUserId()), storeId));
    }

    /**
     * 收藏/取消收藏商品
     * */
    @Login
    @PostMapping("/collectGoods")
    public R collectProduct(@RequestParam Long goodId) {
        return R.ok(liveDataService.collectProduct(Long.parseLong(getUserId()), goodId));
    }

    /**
     * 获取直播内数据
     * */
    @GetMapping("/getLiveViewData/{liveId}")
    public R getLiveViewData(@PathVariable Long liveId) {
        Map<String,Object> liveViewData =liveDataService.getLiveViewData(liveId);
        return R.ok(liveViewData);
    }

    /**
     * 获取发言最多的三个用户
     * */
    @GetMapping("/getRecentLiveViewers/{liveId}")
    public R getRecentLiveViewers(@PathVariable  Long liveId) {
        Map<String, Object> params = new HashMap<>();
        params.put("liveId", liveId);
        List<LiveWatchUserVO> recentLiveViewers = liveWatchUserService.selectWatchUserList(params);
        return R.ok().put("recentLiveViewers", recentLiveViewers);
    }

    /**
     * 获取店铺收藏数
     * */
    @Login
    @GetMapping("/getUserIntegralInfo")
    public R getUserIntegralInfo() {
        String userId = getUserId();
        if (null == userId || StringUtils.isBlank(userId)) {
            return R.error("未找到用户信息");
        }
        FsUser fsUser = fsUserService.selectFsUserById(Long.parseLong(userId));
        HisFsUserVO hisUserIntegral = fsUserService.getHisUserIntegralWithLogs(fsUser);
        return R.ok().put("data", hisUserIntegral);
    }

}
