package com.qqchen.deploy.backend.deploy.service.impl;

import com.qqchen.deploy.backend.deploy.converter.TeamMemberConverter;
import com.qqchen.deploy.backend.deploy.dto.TeamMemberDTO;
import com.qqchen.deploy.backend.deploy.entity.Team;
import com.qqchen.deploy.backend.deploy.entity.TeamMember;
import com.qqchen.deploy.backend.deploy.query.TeamMemberQuery;
import com.qqchen.deploy.backend.deploy.repository.ITeamMemberRepository;
import com.qqchen.deploy.backend.deploy.repository.ITeamRepository;
import com.qqchen.deploy.backend.deploy.service.ITeamMemberService;
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
import com.qqchen.deploy.backend.framework.exception.BusinessException;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import com.qqchen.deploy.backend.system.entity.User;
import com.qqchen.deploy.backend.system.repository.IUserRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TeamMemberServiceImpl extends BaseServiceImpl<TeamMember, TeamMemberDTO, TeamMemberQuery, Long>
        implements ITeamMemberService {

    @Resource
    private ITeamMemberRepository teamMemberRepository;

    @Resource
    private ITeamRepository teamRepository;

    @Resource
    private IUserRepository userRepository;

    @Resource
    private TeamMemberConverter teamMemberConverter;

    @Override
    @Transactional
    public TeamMemberDTO create(TeamMemberDTO dto) {
        // 检查成员是否已加入团队
        if (teamMemberRepository.existsByTeamIdAndUserIdAndDeletedFalse(dto.getTeamId(), dto.getUserId())) {
            throw new BusinessException(ResponseCode.TEAM_MEMBER_ALREADY_EXISTS);
        }

        // 自动填充用户名和加入时间
        if (dto.getUserName() == null && dto.getUserId() != null) {
            Optional<User> userOptional = userRepository.findById(dto.getUserId());
            userOptional.ifPresent(user -> dto.setUserName(user.getUsername()));
        }

        if (dto.getJoinTime() == null) {
            dto.setJoinTime(LocalDateTime.now());
        }

        return super.create(dto);
    }

    @Override
    public Page<TeamMemberDTO> page(TeamMemberQuery query) {
        Page<TeamMemberDTO> page = super.page(query);

        // 填充团队名称
        List<TeamMemberDTO> content = page.getContent().stream()
                .peek(member -> {
                    Optional<Team> teamOptional = teamRepository.findById(member.getTeamId());
                    teamOptional.ifPresent(team -> member.setTeamName(team.getTeamName()));
                })
                .collect(Collectors.toList());

        return new PageImpl<>(content, page.getPageable(), page.getTotalElements());
    }
}

