package com.yaoyicloud.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import com.yaoyicloud.constant.enums.ReportType;
import com.yaoyicloud.service.ReportService;
import com.yaoyicloud.template.AcademicAssociationReport;
import com.yaoyicloud.template.FoundationReport;
import com.yaoyicloud.template.PlatformCompanyReport;

import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
@RequiredArgsConstructor
public class FxyReportServiceImpl implements ReportService {
    private final PlatformCompanyReport platformCompanyReport;
    private final AcademicAssociationReport academicAssociationReport;
    private final FoundationReport foundationReport;


    @SuppressWarnings("checkstyle:ReturnCount")
    @Override
    public String createPlusVersionCheckReport(
        ReportType reportType, Map<String, Object> data, byte[] templateBytes, String outputBasePath, Long relationId)
        throws Exception {

        // 1. 将字节流模版写入临时文件
        File tempTemplateFile = File.createTempFile("template_", ".docx");
        try (FileOutputStream fos = new FileOutputStream(tempTemplateFile)) {
            fos.write(templateBytes);
        }
        Map<String, Object> processedData = null;
        switch (reportType) {
            case PLATFORM_COMPANY:
                 processedData = platformCompanyReport.processData(data);
                return platformCompanyReport.exportReport(
                        processedData,
                    tempTemplateFile.getAbsolutePath(),
                    outputBasePath,
                    relationId);
            case ACADEMIC_ASSOCIATIONS:
                processedData = academicAssociationReport.processData(data);
                return academicAssociationReport.exportReport(
                        processedData,
                        tempTemplateFile.getAbsolutePath(),
                        outputBasePath,
                        relationId);
            case FOUNDATION:
                processedData = foundationReport.processData(data);
                return foundationReport.exportReport(
                        processedData,
                        tempTemplateFile.getAbsolutePath(),
                        outputBasePath,
                        relationId);
            case PROMOTION:
                processedData = platformCompanyReport.processData(data);
                return platformCompanyReport.exportReport(
                        processedData,
                        tempTemplateFile.getAbsolutePath(),
                        outputBasePath,
                        relationId);
            default:
                throw new UnsupportedOperationException("Unsupported report type: " + reportType);
        }
    }


}



