import fs from 'fs';
import path from 'path';
import yaml from 'js-yaml';
import { fileURLToPath } from 'url';

// Get the directory name
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Directory containing YAML files
const databaseDir = path.join(__dirname, 'public', 'data', 'database');

// Get all YAML files in the directory
const yamlFiles = fs.readdirSync(databaseDir).filter(file =>
  file.toLowerCase().endsWith('.yaml')
);

console.log(`Found ${yamlFiles.length} YAML files to convert.`);

// Convert each YAML file to JSON
yamlFiles.forEach(yamlFile => {
  const yamlPath = path.join(databaseDir, yamlFile);
  const jsonFile = yamlFile.replace(/\.ya?ml$/i, '.json');
  const jsonPath = path.join(databaseDir, jsonFile);

  try {
    // Read YAML file
    const yamlContent = fs.readFileSync(yamlPath, 'utf8');

    // Parse YAML to JavaScript object
    const data = yaml.load(yamlContent);

    // Convert to JSON and write to file
    fs.writeFileSync(jsonPath, JSON.stringify(data, null, 2));

    console.log(`Converted ${yamlFile} to ${jsonFile}`);
  } catch (error) {
    console.error(`Error converting ${yamlFile}:`, error);
  }
});

console.log('Conversion complete!');
