/**
 * Centralized directory paths for Forge.
 *
 * Shared (configDir):  ~/.forge/          — only bin/ (cloudflared)
 * Instance (dataDir):  ~/.forge/data/     — settings, db, state, flows, etc.
 *                      or --dir / FORGE_DATA_DIR
 * Claude (claudeDir):  ~/.claude/         — or configured in settings
 */

import { homedir } from 'node:os';
import { join } from 'node:path';
import { existsSync, mkdirSync, renameSync, copyFileSync } from 'node:fs';

/** Shared config directory — only binaries, fixed at ~/.forge/ */
export function getConfigDir(): string {
  return join(homedir(), '.forge');
}

/** Instance data directory — all instance-specific data */
export function getDataDir(): string {
  return process.env.FORGE_DATA_DIR || join(getConfigDir(), 'data');
}

// ─── Migration from old layout (~/.forge/*) to new (~/.forge/data/*) ───

const MIGRATE_FILES = [
  'settings.yaml',
  '.encrypt-key',
  '.env.local',
  'session-code.json',
  'terminal-state.json',
  'tunnel-state.json',
  'preview.json',
  'forge.pid',
  'forge.log',
];
const MIGRATE_DIRS = ['flows', 'pipelines'];

let migrated = false;

/** Migrate old ~/.forge/ flat layout to ~/.forge/data/ if needed */
export function migrateDataDir(): void {
  if (migrated) return;
  migrated = true;

  // Only migrate default data dir, not custom --dir
  if (process.env.FORGE_DATA_DIR) return;

  const configDir = getConfigDir();
  const dataDir = join(configDir, 'data');

  // Check if old layout exists (settings.yaml in root, not in data/)
  const oldSettings = join(configDir, 'settings.yaml');
  const newSettings = join(dataDir, 'settings.yaml');
  if (!existsSync(oldSettings) || existsSync(newSettings)) return;

  console.log('[forge] Migrating data from ~/.forge/ to ~/.forge/data/...');
  if (!existsSync(dataDir)) mkdirSync(dataDir, { recursive: true });

  // Migrate files
  for (const file of MIGRATE_FILES) {
    const src = join(configDir, file);
    const dest = join(dataDir, file);
    if (existsSync(src) && !existsSync(dest)) {
      try { copyFileSync(src, dest); console.log(`  ${file}`); } catch {}
    }
  }

  // Migrate old data.db → data/workflow.db
  const oldDb = join(configDir, 'data.db');
  const newDb = join(dataDir, 'workflow.db');
  if (existsSync(oldDb) && !existsSync(newDb)) {
    try { copyFileSync(oldDb, newDb); console.log('  data.db → workflow.db'); } catch {}
  }

  // Migrate directories
  for (const dir of MIGRATE_DIRS) {
    const src = join(configDir, dir);
    const dest = join(dataDir, dir);
    if (existsSync(src) && !existsSync(dest)) {
      try { renameSync(src, dest); console.log(`  ${dir}/`); } catch {}
    }
  }

  console.log('[forge] Migration complete. Old files kept as backup.');
}

/** Encrypted enterprise keys file — array of { tenant_id, repo_url, github_pat } */
export function getEnterpriseKeysPath(): string {
  return join(getDataDir(), '.enterprise-keys.json');
}

/** Claude config directory — skills, commands, sessions, etc. Override
 *  via the `CLAUDE_HOME` env var (same name claude CLI uses); defaults
 *  to `~/.claude`. The settings.yaml `claudeHome` field was removed in
 *  0.10.10 — set the env var if you need a non-default location. */
export function getClaudeDir(): string {
  return process.env.CLAUDE_HOME || join(homedir(), '.claude');
}
