/**
 * GitFlow Platform Detection.
 * Self-contained: only uses node built-ins.
 */

import { platform as osPlatform } from 'os';
import { readFileSync } from 'fs';
import type { Platform, Shell, PlatformInfo } from './types.js';

export function detectPlatform(): PlatformInfo {
  const osType = osPlatform();

  if (osType === 'linux') {
    try {
      const procVersion = readFileSync('/proc/version', 'utf-8');
      if (/microsoft|wsl/i.test(procVersion)) {
        return { platform: 'wsl', shell: 'bash' };
      }
    } catch { /* Not WSL */ }
    return { platform: 'linux', shell: 'bash' };
  }

  if (osType === 'win32') {
    const ostype = process.env.OSTYPE || '';
    if (/msys|mingw|cygwin/i.test(ostype)) {
      return { platform: 'windows', shell: 'gitbash' };
    }
    return { platform: 'windows', shell: 'gitbash' };
  }

  if (osType === 'darwin') {
    return { platform: 'macos', shell: 'zsh' };
  }

  return { platform: 'unknown', shell: 'bash' };
}

export function isWSL(): boolean {
  return detectPlatform().platform === 'wsl';
}

export function isWindows(): boolean {
  return detectPlatform().platform === 'windows';
}
