package kr.co.accura.wtm.domain.wbs.parser;

import kr.co.accura.wtm.domain.wbs.dto.WbsNodeDto;
import kr.co.accura.wtm.domain.wbs.dto.WbsParseResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;

/**
 * P6 WBS file parser stub.
 * Parses P6 Export files (.xls/.csv) into WBS tree structure.
 * Full implementation will use Apache POI for Excel parsing.
 */
@Service
public class P6WbsParser {

    /**
     * Parse a P6 Export file into WBS nodes.
     * Expected P6 columns:
     * Activity ID | Activity Name | WBS Code | WBS Name | Level | Planned Hours
     */
    public WbsParseResult parse(MultipartFile file) {
        var result = new WbsParseResult();

        try {
            // TODO: Implement Apache POI-based parsing
            // Stub: return empty result for now
            result.setError("P6 WBS parser not yet implemented. Upload a file to test.");
        } catch (Exception e) {
            result.setError("File parsing failed: " + e.getMessage());
        }

        return result;
    }

    /**
     * Derive parent WBS code from a given WBS code.
     * Example: "E.01.03.02.01" (Level 5) -> "E.01.03.02" (Level 4)
     */
    public String deriveParentCode(String wbsCode, int level) {
        if (level <= 1) return null;
        int lastDot = wbsCode.lastIndexOf('.');
        return lastDot > 0 ? wbsCode.substring(0, lastDot) : null;
    }
}
