package com.ycwl.basic.task;

import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.mapper.FaceMapper;
import com.ycwl.basic.mapper.MemberMapper;
import com.ycwl.basic.model.pc.face.req.FaceReqQuery;
import com.ycwl.basic.model.pc.face.resp.FaceRespVO;
import com.ycwl.basic.model.pc.member.resp.MemberRespVO;
import com.ycwl.basic.model.pc.notify.req.WechatSubscribeNotifyTriggerRequest;
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
import com.ycwl.basic.repository.MemberRelationRepository;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.notify.WechatSubscribeNotifyTriggerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
@Slf4j
public class SourceNotificationTasker {

    @Autowired
    private FaceMapper faceMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private WechatSubscribeNotifyTriggerService notifyTriggerService;
    @Autowired
    private ScenicRepository scenicRepository;
    @Autowired
    private MemberRelationRepository memberRelationRepository;

    @Scheduled(cron = "0 0 19 * * ?")
    public void sendSourceNotification() {
        log.info("开始执行SOURCE_NOTICE定时任务");
        Date now = new Date();
        FaceReqQuery query = new FaceReqQuery();
        query.setStartTime(DateUtil.beginOfDay(now));
        query.setEndTime(DateUtil.endOfDay(now));
        
        List<FaceRespVO> faces = faceMapper.list(query);
        log.info("查询到今日新增人脸数: {}", faces.size());
        
        for (FaceRespVO item : faces) {
            try {
                MemberRespVO member = memberMapper.getById(item.getMemberId());
                if (member == null || member.getOpenId() == null) {
                    continue;
                }

                HashMap<String, Object> variables = new HashMap<>();
                ScenicV2DTO scenic = scenicRepository.getScenicBasic(item.getScenicId());
                variables.put("scenicName", scenic.getName());
                variables.put("scenicId", scenic.getId());
                variables.put("faceId", item.getId());
                List<MemberSourceEntity> sourceVideoList = memberRelationRepository.listSourceByFaceRelation(item.getId(), 1);
                variables.put("sourceVideoCount", sourceVideoList.size());
                variables.put("sourceVideoCreateTime", DateUtil.format(item.getCreateAt(), "yyyy-MM-dd HH:mm"));
                List<MemberSourceEntity> sourcePhotoList = memberRelationRepository.listSourceByFaceRelation(item.getId(), 2);
                variables.put("sourcePhotoCount", sourcePhotoList.size());
                variables.put("sourcePhotoCreateTime", DateUtil.format(item.getCreateAt(), "yyyy-MM-dd HH:mm"));

                WechatSubscribeNotifyTriggerRequest request = WechatSubscribeNotifyTriggerRequest.builder()
                        .scenicId(item.getScenicId())
                        .memberId(item.getMemberId())
                        .openId(member.getOpenId())
                        .bizId(String.valueOf(item.getId()))
                        .variables(variables)
                        .build();
                
                notifyTriggerService.trigger("SOURCE_NOTICE", request);
            } catch (Exception e) {
                log.error("发送SOURCE_NOTICE失败, faceId: {}", item.getId(), e);
            }
        }
    }
}
