'use strict';
// Eenvoudige JSON-opslag voor de roster (locs + wissels). Geen database nodig.
const fs = require('fs');
const path = require('path');
const paths = require('./paths');

const FILE = path.join(paths.DATA, 'roster.json');

function load() {
  try {
    return JSON.parse(fs.readFileSync(FILE, 'utf8'));
  } catch (_) {
    return { locos: [], turnouts: [] };
  }
}

function save(roster) {
  try {
    fs.writeFileSync(FILE, JSON.stringify(roster, null, 2));
    return true;
  } catch (_) {
    return false;
  }
}

const LAYOUT = path.join(paths.DATA, 'layout.json');
function loadLayout() {
  try { return JSON.parse(fs.readFileSync(LAYOUT, 'utf8')); }
  catch (_) { return { cols: 14, rows: 8, tiles: {} }; }
}
function saveLayout(layout) {
  try { fs.writeFileSync(LAYOUT, JSON.stringify(layout, null, 2)); return true; }
  catch (_) { return false; }
}

const CONFIG = path.join(paths.DATA, 'config.json');
function saveConfig(c) {
  try { fs.writeFileSync(CONFIG, JSON.stringify(c, null, 2)); return true; }
  catch (_) { return false; }
}

module.exports = { load, save, FILE, loadLayout, saveLayout, LAYOUT, saveConfig, CONFIG };
