/**
 * Parses a query parameter value based on its expected type.
 * Throws error for invalid formats. Adjust date handling as needed.
 */
function parseValue(value, type) {
    if (value === null || value === undefined || value === '') return null;

    console.log(`Parsing value: "${value}" as type: "${type}"`);
    
    switch (type) {
        case 'number':
            const num = parseFloat(value);
            if (isNaN(num)) {
              console.error(`Invalid number format: "${value}"`);
              throw new Error(`Invalid number format: "${value}"`);
            }
            return num;
        case 'integer': // Specific type for integer IDs etc.
             const int = parseInt(value, 10);
             if (isNaN(int)) {
               console.error(`Invalid integer format: "${value}"`);
               throw new Error(`Invalid integer format: "${value}"`);
             }
             console.log(`Successfully parsed integer: ${int}`);
             return int;
        case 'boolean':
            if (String(value).toLowerCase() === 'true') return true;
            if (String(value).toLowerCase() === 'false') return false;
            console.error(`Invalid boolean format: "${value}"`);
            throw new Error(`Invalid boolean format: "${value}"`);
        case 'date':
            // Basic ISO date format validation (YYYY-MM-DD)
            if (!String(value).match(/^\d{4}-\d{2}-\d{2}$/)) {
               console.warn(`Potentially invalid date format passed: "${value}"`);
               // Optionally throw an error or return null depending on strictness
               // throw new Error(`Invalid date format (YYYY-MM-DD expected): "${value}"`);
            }
            return String(value); // Send as string, let DB handle casting/comparison
        case 'string':
        default:
            return String(value);
    }
}

module.exports = { parseValue };