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

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

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

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

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

    @Transactional
    public Map<String, Object> deleteYardLayout(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        Map<String, Object> placementParams = new HashMap<>();
        placementParams.put("yard_layout_id", params.get("id"));
        sqlSession.delete("yardLayout.deleteYardPlacementsByLayoutId", placementParams);
        sqlSession.delete("yardLayout.deleteYardLayout", params);
        return params;
    }

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

    @Transactional
    public Map<String, Object> insertYardPlacement(Map<String, Object> params) {
        sqlSession.insert("yardLayout.insertYardPlacement", params);
        return params;
    }

    @Transactional
    public Map<String, Object> updateYardPlacement(Map<String, Object> params) {
        sqlSession.update("yardLayout.updateYardPlacement", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteYardPlacement(Map<String, Object> params) {
        sqlSession.delete("yardLayout.deleteYardPlacement", params);
        return params;
    }

    @Transactional
    public Map<String, Object> batchUpdateYardPlacements(List<Map<String, Object>> placements) {
        for (Map<String, Object> placement : placements) {
            sqlSession.update("yardLayout.updateYardPlacement", placement);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("updated_count", placements.size());
        return result;
    }

    @Transactional
    public Map<String, Object> duplicateYardLayout(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        Map<String, Object> original = sqlSession.selectOne("yardLayout.getYardLayoutInfo", params);
        if (original == null) {
            throw new RuntimeException("Layout not found: " + params.get("id"));
        }
        Map<String, Object> newLayout = new HashMap<>();
        newLayout.put("name", "Copy of " + original.get("name"));
        newLayout.put("description", original.get("description"));
        newLayout.put("created_by", original.get("created_by"));
        newLayout.put("company_code", params.get("company_code"));
        sqlSession.insert("yardLayout.insertYardLayout", newLayout);
        Long newId = ((Number) newLayout.get("id")).longValue();

        Map<String, Object> placementParams = new HashMap<>();
        placementParams.put("yard_layout_id", params.get("id"));
        List<Map<String, Object>> placements = sqlSession.selectList("yardLayout.getYardPlacementList", placementParams);
        for (Map<String, Object> p : placements) {
            Map<String, Object> newPlacement = new HashMap<>(p);
            newPlacement.remove("id");
            newPlacement.put("yard_layout_id", newId);
            sqlSession.insert("yardLayout.insertYardPlacement", newPlacement);
        }
        newLayout.put("placement_count", placements.size());
        return newLayout;
    }
}
