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

class AquonSamplesMenu {
  constructor(relPath = '../../datasets/assetData') {
    this.baseDir = path.resolve(__dirname, relPath);
    this.samplePath = path.resolve(this.baseDir, 'monsterSamples.json');
    this.specPath = path.resolve(this.baseDir, 'specs/monster/index.json');
    this.cache = new Map();
  }

  _loadJSON(filePath, cacheKey) {
    if (this.cache.has(cacheKey)) {
      return this.cache.get(cacheKey);
    }

    if (!fs.existsSync(filePath)) {
      throw new Error(`Aquon dataset not found: ${filePath}`);
    }

    const raw = fs.readFileSync(filePath, 'utf8');
    const parsed = JSON.parse(raw);
    this.cache.set(cacheKey, parsed);
    return parsed;
  }

  getAllMenuData() {
    const samples = this._loadJSON(this.samplePath, 'samples');
    const specs = this._loadJSON(this.specPath, 'specs');

    return {
      samples: samples.samples || [],
      specs: {
        defaults: specs.defaults || {},
        bySample: specs.bySample || {}
      }
    };
  }
}

module.exports = AquonSamplesMenu;
