#!/usr/bin/env node

/**
 * Unified Chrome MCP setup script.
 *
 * Usage:
 *   node scripts/setup-chrome-mcp.mjs           # Run full setup (fix-permissions → register → doctor)
 *   node scripts/setup-chrome-mcp.mjs doctor    # Run a single sub-command
 */

import { execFileSync } from 'node:child_process'
import { mkdirSync } from 'node:fs'
import { createRequire } from 'node:module'
import { homedir } from 'node:os'
import { join } from 'node:path'

if (process.env.SALUZI_SKIP_CHROME_MCP_SETUP === '1') {
  process.exit(0)
}

const require = createRequire(import.meta.url)

let cliPath
try {
  cliPath = require.resolve('@claude-code-best/mcp-chrome-bridge/dist/cli.js')
} catch {
  // Package not installed — skip silently (non-fatal)
  console.log(
    '[setup-chrome-mcp] @claude-code-best/mcp-chrome-bridge not installed, skipping Chrome MCP setup.',
  )
  process.exit(0)
}

const userArgs = process.argv.slice(2)

function getChromeMcpLogDir() {
  const home = homedir()
  if (process.platform === 'darwin') {
    return join(home, 'Library', 'Logs', 'mcp-chrome-bridge')
  }
  if (process.platform === 'win32') {
    return join(
      process.env.LOCALAPPDATA || join(home, 'AppData', 'Local'),
      'mcp-chrome-bridge',
      'logs',
    )
  }
  return join(
    process.env.XDG_STATE_HOME || join(home, '.local', 'state'),
    'mcp-chrome-bridge',
    'logs',
  )
}

if (userArgs.length > 0) {
  // Forward single sub-command
  try {
    execFileSync('node', [cliPath, ...userArgs], { stdio: 'inherit' })
  } catch (err) {
    console.warn(
      `[setup-chrome-mcp] Command failed (non-fatal): ${err.message}`,
    )
    process.exit(0)
  }
} else {
  // Full setup sequence
  const steps = [
    ['fix-permissions'],
    ['register', '--browser', 'chrome'],
    ['doctor'],
  ]

  try {
    mkdirSync(getChromeMcpLogDir(), { recursive: true })
  } catch {
    // Non-fatal — log dir creation may fail in restricted environments
  }

  for (let i = 0; i < steps.length; i++) {
    const args = steps[i]
    const isLast = i === steps.length - 1
    try {
      if (isLast) console.log(`\n[${i + 1}/${steps.length}] ${args.join(' ')}`)
      execFileSync('node', [cliPath, ...args], {
        stdio: isLast ? 'inherit' : 'pipe',
      })
    } catch (err) {
      console.warn(
        `[setup-chrome-mcp] Step ${args.join(' ')} failed (non-fatal): ${err.message}`,
      )
      // Continue to next step instead of aborting
    }
  }

  console.log('\nChrome MCP setup complete!')
}
