package com.sonic.frog.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.sonic.frog.domain.entity.AiUserHeartbeatRelation;
import com.sonic.frog.domain.entity.MeetUnlock;
import com.sonic.frog.domain.input.MeetUnlockInput;
import com.sonic.frog.service.AiUserHeartbeatRelationService;
import com.sonic.frog.service.MeetService;
import com.sonic.frog.service.MeetUnlockService;
import com.sonic.frog.utils.KeyGenerator;
import com.sonic.lion.lib.client.PayClient;
import com.sonic.lion.lib.enums.BizType;
import com.sonic.lion.lib.input.BalanceCheckoutInput;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class MeetServiceImpl implements MeetService {

    @Autowired
    private PayClient payClient;
    @Autowired
    private AiUserHeartbeatRelationService aiUserHeartbeatRelationService;
    @Autowired
    private MeetUnlockService meetUnlockService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void meetUnLock(Long currentUserId, MeetUnlockInput input) {
        //平台固定费用50
        Long totalAmount = 5000L;
        //生成订单编号
        String orderNo = KeyGenerator.instance().generatorUniqueKey("");
        //调用支付服务扣款
        BalanceCheckoutInput balanceCheckoutInput = BalanceCheckoutInput.builder()
                .platform("Balance")
                .outTradeNo(orderNo)
                .bizType(BizType.UNLOCK_ADMIRERS)
                .name(BizType.UNLOCK_ADMIRERS.getDesc())
                //付款人
                .srcAccountId(currentUserId)
                //收款人
                .desAccountId(-2L)
                //收款总金额
                .productAmount(totalAmount)
                //平台抽取的金额
                .platformFee(0L)
                //折扣金额
                .promoAmount(0L)
                .extend(JSON.toJSONString(input))
                .build();
        payClient.checkoutToUser(balanceCheckoutInput);

        //保存关系数据
        AiUserHeartbeatRelation aiUserHeartbeatRelation = aiUserHeartbeatRelationService.getHeartbeatRelation(currentUserId, input.getAiId());
        if(aiUserHeartbeatRelation == null) {
            //保存记录
            aiUserHeartbeatRelation = new AiUserHeartbeatRelation();
            aiUserHeartbeatRelation.setUserId(currentUserId);
            aiUserHeartbeatRelation.setAiId(input.getAiId());
            aiUserHeartbeatRelation.setCreateTime(LocalDateTime.now());
            aiUserHeartbeatRelation.setEditTime(LocalDateTime.now());
            //首次聊天时间
            aiUserHeartbeatRelation.setFirstChatTime(LocalDateTime.now());
            aiUserHeartbeatRelationService.save(aiUserHeartbeatRelation);
        }
        MeetUnlock meetUnlock = MeetUnlock.builder()
                .userId(currentUserId)
                .aiId(input.getAiId())
                .orderNo(orderNo)
                .createTime(LocalDateTime.now())
                .build();
        //保存解锁记录
        meetUnlockService.save(meetUnlock);
//        //添加meet关系数据
//        userAiMeetService.addMeet(currentUserId, input.getAiId());
    }

}
