#!/usr/bin/env node

import path from 'node:path'

import {
  renderNginxConfig,
  renderSystemdService,
  writeRenderedDeploymentFiles,
} from './render-same-origin-deployment-lib.mjs'

function parseArgs(argv) {
  const options = {
    checkoutRoot: '',
    websiteRoot: '',
    serverRoot: '',
    environmentFile: '',
    serviceUser: 'hypertwist',
    serviceGroup: '',
    nodeEnv: 'production',
    serverNames: '',
    certificateName: '',
    upstreamPort: '3011',
    upstreamName: 'hypertwist_website_auth_server',
    systemdOut: '',
    nginxOut: '',
    stdout: false,
  }

  for (let index = 0; index < argv.length; index += 1) {
    const argument = argv[index]
    const next = argv[index + 1] || ''

    switch (argument) {
      case '--checkout-root':
        options.checkoutRoot = next
        index += 1
        break
      case '--website-root':
        options.websiteRoot = next
        index += 1
        break
      case '--server-root':
        options.serverRoot = next
        index += 1
        break
      case '--environment-file':
        options.environmentFile = next
        index += 1
        break
      case '--service-user':
        options.serviceUser = next
        index += 1
        break
      case '--service-group':
        options.serviceGroup = next
        index += 1
        break
      case '--node-env':
        options.nodeEnv = next
        index += 1
        break
      case '--server-names':
        options.serverNames = next
        index += 1
        break
      case '--certificate-name':
        options.certificateName = next
        index += 1
        break
      case '--upstream-port':
        options.upstreamPort = next
        index += 1
        break
      case '--upstream-name':
        options.upstreamName = next
        index += 1
        break
      case '--systemd-out':
        options.systemdOut = next
        index += 1
        break
      case '--nginx-out':
        options.nginxOut = next
        index += 1
        break
      case '--stdout':
        options.stdout = true
        break
      default:
        break
    }
  }

  return options
}

const args = parseArgs(process.argv.slice(2))
const checkoutRoot = args.checkoutRoot || path.resolve(process.cwd(), '..')

const baseOptions = {
  checkoutRoot,
  websiteRoot: args.websiteRoot,
  serverRoot: args.serverRoot,
  environmentFile: args.environmentFile,
  serviceUser: args.serviceUser,
  serviceGroup: args.serviceGroup,
  nodeEnv: args.nodeEnv,
  serverNames: args.serverNames ? args.serverNames.split(',') : undefined,
  certificateName: args.certificateName,
  upstreamPort: args.upstreamPort,
  upstreamName: args.upstreamName,
}

if (args.systemdOut || args.nginxOut) {
  writeRenderedDeploymentFiles({
    ...baseOptions,
    systemdOut: args.systemdOut,
    nginxOut: args.nginxOut,
  })
}

if (args.stdout || (!args.systemdOut && !args.nginxOut)) {
  process.stdout.write('### systemd\n')
  process.stdout.write(renderSystemdService(baseOptions))
  process.stdout.write('\n### nginx\n')
  process.stdout.write(renderNginxConfig(baseOptions))
}
