/**
 * Parser for multi-line DAT files (DSCLOG, 5BLOG, 8BLOG, PWRLOG, SCTLOG, VASLOG)
 *
 * Format:
 * "MODEL_NUMBER   "
 * measurement1,measurement2,measurement3,measurement4,"PASS/FAIL"
 * ... (test data lines)
 * 0
 * "summary line 1"
 * ...
 * "SERIAL-NUM","MM-DD-YYYY"
 */

const fs = require('fs');
const path = require('path');

/**
 * Parse a multi-line DAT file and extract test records
 * @param {string} filePath - Path to the DAT file
 * @param {string} logType - Type of log (DSCLOG, 5BLOG, etc.)
 * @param {string} testStation - Test station identifier (TS-1L, etc.)
 * @returns {Array} Array of parsed records
 */
function parseMultilineFile(filePath, logType, testStation = null) {
    const records = [];

    try {
        const content = fs.readFileSync(filePath, 'utf8');
        const lines = content.split('\n').map(l => l.trim());

        let currentRecord = [];
        let modelNumber = null;

        for (let i = 0; i < lines.length; i++) {
            const line = lines[i];

            // Skip empty lines
            if (!line) continue;

            // Check if it's a serial/date line (format: "SERIAL","DATE")
            const serialDateMatch = line.match(/^"(\d+-\d+[A-Za-z]?)","(\d{2}-\d{2}-\d{4})"$/);

            if (serialDateMatch) {
                // This is the end of a record
                const serialNumber = serialDateMatch[1];
                const dateStr = serialDateMatch[2];

                if (modelNumber && currentRecord.length > 0) {
                    // Parse date from MM-DD-YYYY to YYYY-MM-DD
                    const [month, day, year] = dateStr.split('-');
                    const testDate = `${year}-${month}-${day}`;

                    // Determine overall result from raw data
                    const rawData = currentRecord.join('\n');
                    const overallResult = determineResult(rawData);

                    records.push({
                        log_type: logType,
                        model_number: modelNumber.trim(),
                        serial_number: serialNumber,
                        test_date: testDate,
                        test_station: testStation,
                        overall_result: overallResult,
                        raw_data: rawData,
                        source_file: filePath
                    });
                }

                // Reset for next record
                currentRecord = [];
                modelNumber = null;
            }
            // Check if this is a model number line
            // Model numbers: single quoted string with product code (letters+numbers, possibly with dash)
            // Examples: "DSCA38-1793  ", "SCM5B30-01     ", "8B30-01        "
            else if (/^"[A-Z0-9]+[A-Z0-9-]*\s*"$/.test(line) && !line.includes(',') && !line.includes('PASS') && !line.includes('FAIL')) {
                // This is a model number line - start new record
                if (currentRecord.length > 0 && modelNumber) {
                    // Previous record didn't have serial/date - skip it
                    currentRecord = [];
                }
                modelNumber = line.replace(/"/g, '').trim();
                currentRecord.push(line);
            } else {
                // Add line to current record
                currentRecord.push(line);
            }
        }
    } catch (err) {
        console.error(`Error parsing ${filePath}: ${err.message}`);
    }

    return records;
}

/**
 * Determine overall PASS/FAIL result from raw data
 */
function determineResult(rawData) {
    const failCount = (rawData.match(/"FAIL/gi) || []).length;
    const passCount = (rawData.match(/"PASS/gi) || []).length;

    if (failCount > 0) return 'FAIL';
    if (passCount > 0) return 'PASS';
    return 'UNKNOWN';
}

/**
 * Extract test station from file path
 */
function extractTestStation(filePath) {
    const match = filePath.match(/TS-\d+[LR]/i);
    return match ? match[0].toUpperCase() : null;
}

module.exports = {
    parseMultilineFile,
    extractTestStation
};
