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

import com.qqchen.deploy.backend.deploy.converter.ExternalSystemConverter;
import com.qqchen.deploy.backend.deploy.converter.ProjectGroupConverter;
import com.qqchen.deploy.backend.deploy.converter.RepositoryGroupConverter;
import com.qqchen.deploy.backend.deploy.converter.RepositoryProjectConverter;
import com.qqchen.deploy.backend.deploy.dto.ApplicationDTO;
import com.qqchen.deploy.backend.deploy.dto.DevelopmentLanguageTypeDTO;
import com.qqchen.deploy.backend.deploy.entity.Application;
import com.qqchen.deploy.backend.deploy.entity.ExternalSystem;
import com.qqchen.deploy.backend.deploy.entity.ProjectGroup;
import com.qqchen.deploy.backend.deploy.entity.RepositoryGroup;
import com.qqchen.deploy.backend.deploy.entity.RepositoryProject;
import com.qqchen.deploy.backend.deploy.enums.DevelopmentLanguageTypeEnum;
import com.qqchen.deploy.backend.deploy.query.ApplicationQuery;
import com.qqchen.deploy.backend.deploy.repository.IApplicationRepository;
import com.qqchen.deploy.backend.deploy.repository.IExternalSystemRepository;
import com.qqchen.deploy.backend.deploy.repository.IProjectGroupRepository;
import com.qqchen.deploy.backend.deploy.repository.IRepositoryGroupRepository;
import com.qqchen.deploy.backend.deploy.repository.IRepositoryProjectRepository;
import com.qqchen.deploy.backend.deploy.service.IApplicationService;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

@Service
public class ApplicationServiceImpl extends BaseServiceImpl<Application, ApplicationDTO, ApplicationQuery, Long> implements IApplicationService {

    @Resource
    private IProjectGroupRepository projectGroupRepository;

    @Resource
    private ProjectGroupConverter projectGroupConverter;

    @Resource
    private IRepositoryGroupRepository repositoryGroupRepository;

    @Resource
    private IRepositoryProjectRepository repositoryProjectRepository;

    @Resource
    private RepositoryGroupConverter repositoryGroupConverter;

    @Resource
    private RepositoryProjectConverter repositoryProjectConverter;

    @Resource
    private IExternalSystemRepository externalSystemRepository;

    @Resource
    private ExternalSystemConverter externalSystemConverter;

    @Resource
    private IApplicationRepository applicationRepository;

    public Page<ApplicationDTO> page(ApplicationQuery query) {
        Page<ApplicationDTO> page = super.page(query);
        List<ApplicationDTO> result = page.getContent().stream().peek(application -> {
            // 查询并设置项目组信息
            Optional<ProjectGroup> projectGroupOptional = projectGroupRepository.findById(application.getProjectGroupId());
            projectGroupOptional.ifPresent(projectGroup -> application.setProjectGroup(projectGroupConverter.toDto(projectGroup)));

            // 查询并设置代码仓库组信息
            Optional<RepositoryGroup> repositoryGroupOptional = repositoryGroupRepository.findByRepoGroupId(application.getRepoGroupId());
            repositoryGroupOptional.ifPresent(repositoryGroup -> application.setRepositoryGroup(repositoryGroupConverter.toDto(repositoryGroup)));

            // 查询并设置代码仓库项目信息
            Optional<RepositoryProject> repositoryProjectOptional = repositoryProjectRepository.findByRepoProjectId(application.getRepoProjectId());
            repositoryProjectOptional.ifPresent(repositoryProject -> application.setRepositoryProject(repositoryProjectConverter.toDto(repositoryProject)));

            // 查询并设置外部系统信息
            Optional<ExternalSystem> externalSystemOptional = externalSystemRepository.findById(application.getExternalSystemId());
            externalSystemOptional.ifPresent(externalSystem -> application.setExternalSystem(externalSystemConverter.toDto(externalSystem)));
        }).collect(toList());
        return new PageImpl<>(result, page.getPageable(), page.getTotalElements());
    }


    @Override
    public List<DevelopmentLanguageTypeDTO> getAllDevelopmentLanguageTypes() {
        List<DevelopmentLanguageTypeDTO> result = new ArrayList<>();
        Arrays.stream(DevelopmentLanguageTypeEnum.values()).forEach(developmentLanguageTypeEnum -> {
            DevelopmentLanguageTypeDTO dto = new DevelopmentLanguageTypeDTO();
            dto.setCode(developmentLanguageTypeEnum.name());
            dto.setName(developmentLanguageTypeEnum.getCode());
            result.add(dto);
        });
        return result;
    }

} 