const fs = require('fs');

module.exports = function parseCrzy(filepath) {
  const lines = fs.readFileSync(filepath, 'utf-8').split('\n');
  const config = {};
  let section = null;
  const casters = { string: String, int: Number, bool: v => v === 'true', path: String };

  for (const raw of lines) {
    const line = raw.trim();
    if (!line || line.startsWith('#')) continue;
    if (line.startsWith('@section')) { section = line.split(/\s+/)[1]; config[section] = {}; continue; }
    const match = line.match(/^(\w+)\s*::\s*(\w+)\s*=\s*(.+)$/);
    if (!match || !section) continue;
    const [, key, type, val] = match;
    const clean = val.trim().replace(/^["']|["']$/g, '');
    config[section][key] = (casters[type] || String)(clean);
  }
  return config;
};