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 WorkHistoryService extends BaseService {
    @Autowired private CommonService commonService;

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

    public Map<String, Object> getWorkHistoryInfo(Map<String, Object> params) {
        return sqlSession.selectOne("workHistory.getWorkHistoryInfo", params);
    }

    @Transactional
    public Map<String, Object> insertWorkHistory(Map<String, Object> params) {
        sqlSession.insert("workHistory.insertWorkHistory", params);
        return sqlSession.selectOne("workHistory.getWorkHistoryInfo", Map.of("id", params.get("id")));
    }

    @Transactional
    public Map<String, Object> updateWorkHistory(Map<String, Object> params) {
        sqlSession.update("workHistory.updateWorkHistory", params);
        return sqlSession.selectOne("workHistory.getWorkHistoryInfo", Map.of("id", params.get("id")));
    }

    @Transactional
    public void deleteWorkHistory(Map<String, Object> params) {
        sqlSession.delete("workHistory.deleteWorkHistory", params);
    }

    public Map<String, Object> getWorkHistoryStats() {
        Map<String, Object> p = new HashMap<>();
        Map<String, Object> todayStats = sqlSession.selectOne("workHistory.getWorkHistoryTodayStats", p);
        Map<String, Object> totalStats = sqlSession.selectOne("workHistory.getWorkHistoryTotalStats", p);
        Map<String, Object> onTimeStats = sqlSession.selectOne("workHistory.getWorkHistoryOnTimeRate", p);
        List<Map<String, Object>> typeList = sqlSession.selectList("workHistory.getWorkHistoryTypeDistribution", p);

        Map<String, Object> typeDistribution = new LinkedHashMap<>();
        typeDistribution.put("inbound", 0);
        typeDistribution.put("outbound", 0);
        typeDistribution.put("transfer", 0);
        typeDistribution.put("maintenance", 0);
        for (Map<String, Object> row : typeList) {
            String type = String.valueOf(row.get("work_type"));
            typeDistribution.put(type, row.get("count"));
        }

        Map<String, Object> result = new LinkedHashMap<>();
        result.put("today_total", todayStats != null ? todayStats.get("today_total") : 0);
        result.put("today_completed", todayStats != null ? todayStats.get("today_completed") : 0);
        result.put("total_weight", totalStats != null ? totalStats.get("total_weight") : 0);
        result.put("total_distance", totalStats != null ? totalStats.get("total_distance") : 0);
        result.put("on_time_rate", onTimeStats != null ? onTimeStats.get("on_time_rate") : 0);
        result.put("type_distribution", typeDistribution);
        return result;
    }

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

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