import { readFileSync } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));

function loadConfig() {
  return JSON.parse(readFileSync(join(__dirname, "..", "config.json"), "utf-8"));
}

export function getFactorioServer() {
  const config = loadConfig();
  return config.servers.find(s => s.type === "factorio");
}

// Default map-gen-settings structure
export function getDefaultMapGenSettings() {
  return {
    terrain_segmentation: 1,
    water: 1,
    width: 0,
    height: 0,
    starting_area: 1,
    peaceful_mode: false,
    autoplace_controls: {
      coal: { frequency: 1, size: 1, richness: 1 },
      stone: { frequency: 1, size: 1, richness: 1 },
      "copper-ore": { frequency: 1, size: 1, richness: 1 },
      "iron-ore": { frequency: 1, size: 1, richness: 1 },
      "uranium-ore": { frequency: 1, size: 1, richness: 1 },
      "crude-oil": { frequency: 1, size: 1, richness: 1 },
      trees: { frequency: 1, size: 1, richness: 1 },
      "enemy-base": { frequency: 1, size: 1, richness: 1 }
    },
    cliff_settings: {
      name: "cliff",
      cliff_elevation_0: 10,
      cliff_elevation_interval: 40,
      richness: 1
    },
    seed: null
  };
}

// Factorio presets
export const FACTORIO_PRESETS = {
  default: getDefaultMapGenSettings(),
  "rich-resources": {
    ...getDefaultMapGenSettings(),
    autoplace_controls: {
      coal: { frequency: 1, size: 1, richness: 2 },
      stone: { frequency: 1, size: 1, richness: 2 },
      "copper-ore": { frequency: 1, size: 1, richness: 2 },
      "iron-ore": { frequency: 1, size: 1, richness: 2 },
      "uranium-ore": { frequency: 1, size: 1, richness: 2 },
      "crude-oil": { frequency: 1, size: 1, richness: 2 },
      trees: { frequency: 1, size: 1, richness: 1 },
      "enemy-base": { frequency: 1, size: 1, richness: 1 }
    }
  },
  "rail-world": {
    ...getDefaultMapGenSettings(),
    autoplace_controls: {
      coal: { frequency: 0.33, size: 3, richness: 1 },
      stone: { frequency: 0.33, size: 3, richness: 1 },
      "copper-ore": { frequency: 0.33, size: 3, richness: 1 },
      "iron-ore": { frequency: 0.33, size: 3, richness: 1 },
      "uranium-ore": { frequency: 0.33, size: 3, richness: 1 },
      "crude-oil": { frequency: 0.33, size: 3, richness: 1 },
      trees: { frequency: 1, size: 1, richness: 1 },
      "enemy-base": { frequency: 0.5, size: 1, richness: 1 }
    }
  },
  "death-world": {
    ...getDefaultMapGenSettings(),
    autoplace_controls: {
      coal: { frequency: 1, size: 1, richness: 1 },
      stone: { frequency: 1, size: 1, richness: 1 },
      "copper-ore": { frequency: 1, size: 1, richness: 1 },
      "iron-ore": { frequency: 1, size: 1, richness: 1 },
      "uranium-ore": { frequency: 1, size: 1, richness: 1 },
      "crude-oil": { frequency: 1, size: 1, richness: 1 },
      trees: { frequency: 1, size: 1, richness: 1 },
      "enemy-base": { frequency: 2, size: 2, richness: 1 }
    }
  },
  peaceful: {
    ...getDefaultMapGenSettings(),
    peaceful_mode: true
  }
};

export function getPresetNames() {
  return Object.keys(FACTORIO_PRESETS);
}

export function getPreset(name) {
  return FACTORIO_PRESETS[name] || FACTORIO_PRESETS.default;
}
