package com.red.circle.other.app.command.activity.query;

import com.red.circle.other.app.dto.clientobject.activity.MyInviterInfoCO;
import com.red.circle.other.infra.database.rds.entity.user.user.UserInviteUser;
import com.red.circle.other.infra.database.rds.service.user.user.UserInviteUserService;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Objects;

/**
 * 查询我的邀请人信息执行器
 *
 * @author Claude
 * @since 2025-10-28
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MyInviterInfoQryExe {

    private final UserInviteUserService inviteUserService;
    private final UserProfileClient userProfileClient;

    public MyInviterInfoCO execute(Long userId) {
        // 1. 获取当前用户信息
        UserProfileDTO currentUser = userProfileClient.getByUserId(userId).getBody();
        if (currentUser == null) {
            log.warn("用户不存在, userId: {}", userId);
            return null;
        }

        // 2. 构建返回对象 - 当前用户信息
        MyInviterInfoCO result = new MyInviterInfoCO();
        MyInviterInfoCO.UserBasicInfoCO userBasicInfoCO = new MyInviterInfoCO.UserBasicInfoCO();
        userBasicInfoCO.setUserId(currentUser.getId());
        userBasicInfoCO.setUserName(currentUser.getUserNickname());
        userBasicInfoCO.setAvatar(currentUser.getUserAvatar());
        userBasicInfoCO.setCreateTime(currentUser.getCreateTime());

        // 校验用户创建时间是否超过7天
        Timestamp createTime = currentUser.getCreateTime();
        if (createTime != null) {
          long createTimeMillis = createTime.getTime();
          long currentTime = TimestampUtils.now().getTime();
          long threeDaysInMillis = 7 * 24 * 60 * 60 * 1000L; // 7天的毫秒数
          long timeDiff = currentTime - createTimeMillis;

          if (timeDiff > threeDaysInMillis) {
            userBasicInfoCO.setExpired(true);
          }
        }

        result.setMyInfo(userBasicInfoCO);

        // 3. 查询邀请关系
        UserInviteUser inviteRelation = inviteUserService.query()
            .eq(UserInviteUser::getInviteUserId, userId)
            .last("limit 1")
            .getOne();
        if (inviteRelation == null || inviteRelation.getInviteUserId() == null) {
            // 没有邀请人，直接返回
            return result;
        }

        // 4. 获取邀请人信息
        Long inviterUserId = inviteRelation.getUserId();
        UserProfileDTO inviterUser = userProfileClient.getByUserId(inviterUserId).getBody();
        if (inviterUser != null) {
            MyInviterInfoCO.UserBasicInfoCO inviter = new MyInviterInfoCO.UserBasicInfoCO();
            inviter.setUserId(inviterUser.getId());
            inviter.setUserName(inviterUser.getUserNickname());
            inviter.setAvatar(inviterUser.getUserAvatar());
            inviter.setCreateTime(inviterUser.getCreateTime());
            result.setInviterInfo(inviter);
        }

        return result;
    }
}