package com.erp.service;
import com.erp.common.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@Slf4j
public class CollectionService extends BaseService {
    @Autowired private CommonService commonService;

    public Map<String, Object> getCollectionList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        commonService.applyPagination(params);
        Number cntNum = sqlSession.selectOne("collection.getCollectionListCnt", params);
        int totalCount = cntNum != null ? cntNum.intValue() : 0;
        List<Map<String, Object>> list = sqlSession.selectList("collection.getCollectionList", params);
        return commonService.buildListResponse(list, totalCount, params);
    }

    public Map<String, Object> getCollectionInfo(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectOne("collection.getCollectionInfo", params);
    }

    @Transactional
    public Map<String, Object> insertCollection(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert("collection.insertCollection", params);
        return sqlSession.selectOne("collection.getCollectionInfo", params);
    }

    @Transactional
    public Map<String, Object> updateCollection(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update("collection.updateCollection", params);
        return sqlSession.selectOne("collection.getCollectionInfo", params);
    }

    @Transactional
    public void deleteCollection(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete("collection.deleteCollection", params);
    }

    @Transactional
    public Map<String, Object> executeCollection(Map<String, Object> params) {
        Map<String, Object> job = new HashMap<>();
        job.put("config_id", params.get("id"));
        job.put("job_status", "running");
        sqlSession.insert("collection.insertCollectionJob", job);
        try {
            job.put("job_status", "completed");
            job.put("records_processed", 0);
            sqlSession.update("collection.updateCollectionJob", job);
            return job;
        } catch (Exception e) {
            job.put("job_status", "failed");
            job.put("error_message", e.getMessage());
            sqlSession.update("collection.updateCollectionJob", job);
            throw e;
        }
    }

    public Map<String, Object> getCollectionJobList(Map<String, Object> params) {
        commonService.applyPagination(params);
        List<Map<String, Object>> list = sqlSession.selectList("collection.getCollectionJobList", params);
        return commonService.buildListResponse(list, list.size(), params);
    }

    public List<Map<String, Object>> getCollectionHistory(Map<String, Object> params) {
        return sqlSession.selectList("collection.getCollectionHistory", params);
    }
}
