// Copyright (c) 2025 Gordon Bolton. MIT License.
const { logToFile } = require('./logger');

// Validate that a configured service URL is well-formed and uses http(s).
// Emits a warning (never throws) so a misconfigured instance degrades
// gracefully rather than crashing the whole server.
function validateInstanceUrl(url, instanceId) {
  if (!url || typeof url !== 'string') {
    logToFile(`[Config] WARNING: instance "${instanceId}" has no URL configured`);
    return false;
  }
  let parsed;
  try {
    parsed = new URL(url);
  } catch {
    logToFile(`[Config] WARNING: instance "${instanceId}" has an invalid URL: "${url}"`);
    return false;
  }
  if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
    logToFile(`[Config] WARNING: instance "${instanceId}" URL must use http or https, got "${parsed.protocol}"`);
    return false;
  }
  return true;
}

function parseInstances(envVar, legacyUrl, legacyKey, legacyUsername, legacyPassword) {
  // Try to parse JSON array format first
  if (envVar) {
    try {
      // Handle multi-line JSON by removing newlines and extra spaces
      const cleaned = envVar.replace(/\s+/g, ' ').trim();
      const instances = JSON.parse(cleaned);
      if (Array.isArray(instances) && instances.length > 0) {
        logToFile(`[Config] Parsed ${instances.length} instances from JSON array`);
        return instances.map((inst, idx) => {
          const id = inst.name || `instance-${idx + 1}`;
          validateInstanceUrl(inst.url, id);
          return { ...inst, id };
        });
      }
    } catch (err) {
      logToFile(`[Config] Failed to parse JSON array: ${err.message}`);
    }
  }
  
  // Fall back to legacy single-instance format
  if (legacyUrl && legacyKey) {
    logToFile(`[Config] Using legacy single-instance format`);
    validateInstanceUrl(legacyUrl, 'default');
    return [{
      id: 'default',
      name: 'Default',
      url: legacyUrl,
      apiKey: legacyKey,
      username: legacyUsername,
      password: legacyPassword
    }];
  }
  
  return [];
}

function getSABnzbdInstances() {
  return parseInstances(
    process.env.SABNZBD_INSTANCES,
    process.env.SABNZBD_URL,
    process.env.SABNZBD_API_KEY
  );
}

function getSonarrInstances() {
  return parseInstances(
    process.env.SONARR_INSTANCES,
    process.env.SONARR_URL,
    process.env.SONARR_API_KEY
  );
}

function getRadarrInstances() {
  return parseInstances(
    process.env.RADARR_INSTANCES,
    process.env.RADARR_URL,
    process.env.RADARR_API_KEY
  );
}

function getQbittorrentInstances() {
  return parseInstances(
    process.env.QBITTORRENT_INSTANCES,
    process.env.QBITTORRENT_URL,
    null, // no apiKey for qBittorrent
    process.env.QBITTORRENT_USERNAME,
    process.env.QBITTORRENT_PASSWORD
  );
}

module.exports = {
  getSABnzbdInstances,
  getSonarrInstances,
  getRadarrInstances,
  getQbittorrentInstances,
  parseInstances,
  validateInstanceUrl
};
