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 MoldService extends BaseService {

    @Autowired private CommonService commonService;

    // ── 금형 마스터 ───────────────────────────────────────────────────────────

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

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

    @Transactional
    public Map<String, Object> insertMold(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert("mold.insertMold", params);
        return sqlSession.selectOne("mold.getMoldInfo", params);
    }

    @Transactional
    public Map<String, Object> updateMold(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        int updated = sqlSession.update("mold.updateMold", params);
        if (updated == 0) return null;
        return sqlSession.selectOne("mold.getMoldInfo", params);
    }

    @Transactional
    public boolean deleteMold(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete("mold.deleteMoldSerialByMoldCode", params);
        sqlSession.delete("mold.deleteMoldInspectionByMoldCode", params);
        sqlSession.delete("mold.deleteMoldPartByMoldCode", params);
        return sqlSession.delete("mold.deleteMold", params) > 0;
    }

    // ── 일련번호 ──────────────────────────────────────────────────────────────

    public List<Map<String, Object>> getMoldSerialList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectList("mold.getMoldSerialList", params);
    }

    @Transactional
    public Map<String, Object> insertMoldSerial(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        if (params.get("status") == null) params.put("status", "STORED");
        if (params.get("progress") == null) params.put("progress", 0);
        sqlSession.insert("mold.insertMoldSerial", params);
        return params;
    }

    @Transactional
    public boolean deleteMoldSerial(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.delete("mold.deleteMoldSerial", params) > 0;
    }

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

    // ── 점검항목 ──────────────────────────────────────────────────────────────

    public List<Map<String, Object>> getMoldInspectionList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectList("mold.getMoldInspectionList", params);
    }

    @Transactional
    public Map<String, Object> insertMoldInspection(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        if (params.get("is_active") == null) params.put("is_active", "Y");
        sqlSession.insert("mold.insertMoldInspection", params);
        return params;
    }

    @Transactional
    public boolean deleteMoldInspection(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.delete("mold.deleteMoldInspection", params) > 0;
    }

    // ── 부품 ──────────────────────────────────────────────────────────────────

    public List<Map<String, Object>> getMoldPartList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectList("mold.getMoldPartList", params);
    }

    @Transactional
    public Map<String, Object> insertMoldPart(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert("mold.insertMoldPart", params);
        return params;
    }

    @Transactional
    public boolean deleteMoldPart(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.delete("mold.deleteMoldPart", params) > 0;
    }
}
