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

/**
 * Detects the current operating system
 * @returns {string} 'windows', 'macos', or 'linux'
 */
function detectOS() {
  const platform = process.platform;
  
  switch (platform) {
    case 'win32':
      return 'windows';
    case 'darwin':
      return 'macos';
    case 'linux':
      return 'linux';
    default:
      return 'unknown';
  }
}

/**
 * Detects the system architecture
 * @returns {string} 'x64', 'arm64', etc.
 */
function detectArchitecture() {
  return process.arch;
}

/**
 * Detects the appropriate shell for the platform
 * @returns {string} 'powershell', 'bash', or 'zsh'
 */
function detectShell() {
  const platform = process.platform;
  
  if (platform === 'win32') {
    return 'powershell';
  }
  
  // Check for zsh on macOS (default since Catalina)
  if (platform === 'darwin') {
    const shell = process.env.SHELL || '';
    if (shell.includes('zsh')) {
      return 'zsh';
    }
    return 'bash';
  }
  
  // Linux typically uses bash
  return 'bash';
}

/**
 * Gets the default installation path for the current platform
 * @returns {string} Default installation path
 */
function getDefaultInstallPath() {
  const platform = process.platform;
  
  switch (platform) {
    case 'win32':
      return 'C:\\Program Files\\DashCaddy';
    case 'darwin':
      return '/Applications/DashCaddy';
    case 'linux':
      // Use /opt for system-wide or home directory for user install
      return '/opt/dashcaddy';
    default:
      return path.join(os.homedir(), 'DashCaddy');
  }
}

/**
 * Gets the user's home directory installation path
 * @returns {string} User home installation path
 */
function getUserInstallPath() {
  return path.join(os.homedir(), 'DashCaddy');
}

/**
 * Normalizes a file path for the current platform
 * @param {string} filePath - Path to normalize
 * @returns {string} Normalized path
 */
function normalizePath(filePath) {
  return path.normalize(filePath);
}

/**
 * Joins path segments using the correct separator for the platform
 * @param {...string} segments - Path segments to join
 * @returns {string} Joined path
 */
function joinPath(...segments) {
  return path.join(...segments);
}

/**
 * Gets platform-specific information
 * @returns {Object} Platform information
 */
function getPlatformInfo() {
  return {
    os: detectOS(),
    arch: detectArchitecture(),
    shell: detectShell(),
    platform: process.platform,
    hostname: os.hostname(),
    homedir: os.homedir(),
    defaultInstallPath: getDefaultInstallPath(),
    userInstallPath: getUserInstallPath()
  };
}

/**
 * Checks if running on Windows
 * @returns {boolean}
 */
function isWindows() {
  return process.platform === 'win32';
}

/**
 * Checks if running on macOS
 * @returns {boolean}
 */
function isMacOS() {
  return process.platform === 'darwin';
}

/**
 * Checks if running on Linux
 * @returns {boolean}
 */
function isLinux() {
  return process.platform === 'linux';
}

/**
 * Gets the command to execute based on platform
 * @param {string} command - Base command
 * @returns {Object} Command execution details
 */
function getCommandExecution(command) {
  if (isWindows()) {
    return {
      shell: 'powershell.exe',
      args: ['-Command', command]
    };
  }
  
  return {
    shell: detectShell(),
    args: ['-c', command]
  };
}

module.exports = {
  detectOS,
  detectArchitecture,
  detectShell,
  getDefaultInstallPath,
  getUserInstallPath,
  normalizePath,
  joinPath,
  getPlatformInfo,
  isWindows,
  isMacOS,
  isLinux,
  getCommandExecution
};
