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

import com.qqchen.deploy.backend.deploy.dto.JenkinsInstanceDTO;
import com.qqchen.deploy.backend.deploy.dto.JenkinsJobDTO;
import com.qqchen.deploy.backend.deploy.dto.JenkinsViewDTO;
import com.qqchen.deploy.backend.deploy.entity.JenkinsJob;
import com.qqchen.deploy.backend.deploy.entity.JenkinsView;
import com.qqchen.deploy.backend.deploy.service.IJenkinsManagerService;
import com.qqchen.deploy.backend.deploy.service.IJenkinsViewService;
import com.qqchen.deploy.backend.deploy.service.IJenkinsJobService;
import com.qqchen.deploy.backend.deploy.service.IJenkinsBuildService;
import com.qqchen.deploy.backend.deploy.service.IJenkinsSyncHistoryService;
import com.qqchen.deploy.backend.deploy.entity.JenkinsSyncHistory;
import com.qqchen.deploy.backend.deploy.enums.ExternalSystemSyncStatus;
import com.qqchen.deploy.backend.deploy.enums.JenkinsSyncType;
import com.qqchen.deploy.backend.deploy.repository.IJenkinsSyncHistoryRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Jenkins管理服务实现
 */
@Slf4j
@Service
public class JenkinsManagerServiceImpl implements IJenkinsManagerService {

    @Resource
    private IJenkinsViewService jenkinsViewService;

    @Resource
    private IJenkinsJobService jenkinsJobService;

    @Resource
    private IJenkinsBuildService jenkinsBuildService;

    @Resource
    private IJenkinsSyncHistoryRepository jenkinsSyncHistoryRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncAll(Long externalSystemId) {
        // 同步视图
        syncViews(externalSystemId);
        // 同步任务
        syncJobs(externalSystemId);
        // 同步构建信息
        syncBuilds(externalSystemId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncViews(Long externalSystemId) {
        // 同步视图
        Integer viewCount = jenkinsViewService.syncViews(externalSystemId);
        log.info("Successfully synchronized {} views", viewCount);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncJobs(Long externalSystemId) {
        // 同步任务
        Integer jobCount = jenkinsJobService.syncJobs(externalSystemId);
        log.info("Successfully synchronized {} jobs", jobCount);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncBuilds(Long externalSystemId) {
        // 同步构建信息
        Integer buildCount = jenkinsBuildService.syncAllBuilds(externalSystemId);
        log.info("Successfully synchronized {} builds", buildCount);
    }

    @Override
    public JenkinsInstanceDTO instance(Long externalSystemId) {
        // 1. 创建返回对象
        JenkinsInstanceDTO instanceDTO = new JenkinsInstanceDTO();

        // 2. 获取视图列表
        List<JenkinsViewDTO> views = jenkinsViewService.findByExternalSystemId(externalSystemId);
        instanceDTO.setJenkinsViewList(views);
        instanceDTO.setTotalViews(views.size());

        // 3. 获取任务列表
        List<JenkinsJobDTO> jobs = jenkinsJobService.findByExternalSystemId(externalSystemId);
        instanceDTO.setJenkinsJobList(jobs);
        instanceDTO.setTotalJobs(jobs.size());

        // 4. 获取构建总数
        Long totalBuilds = jenkinsBuildService.countByExternalSystemId(externalSystemId);
        instanceDTO.setTotalBuilds(totalBuilds.intValue());

        // 5. 获取最后同步时间
        JenkinsSyncHistory lastViewSync = jenkinsSyncHistoryRepository.findTopByExternalSystemIdAndSyncTypeAndStatusOrderByStartTimeDesc(
            externalSystemId, JenkinsSyncType.VIEW, ExternalSystemSyncStatus.SUCCESS);
        if (lastViewSync != null) {
            instanceDTO.setLastSyncViewsTime(lastViewSync.getEndTime());
        }

        JenkinsSyncHistory lastJobSync = jenkinsSyncHistoryRepository.findTopByExternalSystemIdAndSyncTypeAndStatusOrderByStartTimeDesc(
            externalSystemId, JenkinsSyncType.JOB, ExternalSystemSyncStatus.SUCCESS);
        if (lastJobSync != null) {
            instanceDTO.setLastSyncJobsTime(lastJobSync.getEndTime());
        }

        JenkinsSyncHistory lastBuildSync = jenkinsSyncHistoryRepository.findTopByExternalSystemIdAndSyncTypeAndStatusOrderByStartTimeDesc(
            externalSystemId, JenkinsSyncType.BUILD, ExternalSystemSyncStatus.SUCCESS);
        if (lastBuildSync != null) {
            instanceDTO.setLastSyncBuildsTime(lastBuildSync.getEndTime());
        }

        return instanceDTO;
    }
}