package com.erp.service;

import com.erp.common.BaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class PackagingService extends BaseService {

    private static final String NS = "packaging.";

    private final CommonService commonService;

    // ── 포장단위 (pkg_unit) ──────────────────────────────────────────────

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

    @Transactional
    public Map<String, Object> createPkgUnit(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert(NS + "insertPkgUnit", params);
        return params;
    }

    @Transactional
    public Map<String, Object> updatePkgUnit(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "updatePkgUnit", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deletePkgUnit(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete(NS + "deletePkgUnitItemsByUnitId", params);
        sqlSession.delete(NS + "deletePkgUnit", params);
        return params;
    }

    // ── 포장단위 매칭품목 (pkg_unit_item) ────────────────────────────────

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

    @Transactional
    public Map<String, Object> createPkgUnitItem(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert(NS + "insertPkgUnitItem", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deletePkgUnitItem(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete(NS + "deletePkgUnitItem", params);
        return params;
    }

    // ── 적재함 (loading_unit) ────────────────────────────────────────────

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

    @Transactional
    public Map<String, Object> createLoadingUnit(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert(NS + "insertLoadingUnit", params);
        return params;
    }

    @Transactional
    public Map<String, Object> updateLoadingUnit(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update(NS + "updateLoadingUnit", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteLoadingUnit(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete(NS + "deleteLoadingUnitPkgsByUnitId", params);
        sqlSession.delete(NS + "deleteLoadingUnit", params);
        return params;
    }

    // ── 적재함 포장구성 (loading_unit_pkg) ───────────────────────────────

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

    @Transactional
    public Map<String, Object> createLoadingUnitPkg(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert(NS + "insertLoadingUnitPkg", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteLoadingUnitPkg(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete(NS + "deleteLoadingUnitPkg", params);
        return params;
    }
}
