/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2026 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
 */

import { createServer } from 'node:http'
import http from 'node:http'
import { promises as fs } from 'node:fs'
import net from 'node:net'
import path from 'node:path'

function parseArgs(argv) {
  const options = {
    host: '127.0.0.1',
    port: 4173,
    root: path.resolve('dist'),
    wsProxyHost: null,
    wsProxyPort: null,
  }

  for (let i = 0; i < argv.length; i += 1) {
    const arg = argv[i]
    if (arg === '--host') options.host = argv[++i]
    if (arg === '--port') options.port = Number(argv[++i])
    if (arg === '--root') options.root = path.resolve(argv[++i])
    if (arg === '--ws-proxy-host') options.wsProxyHost = argv[++i]
    if (arg === '--ws-proxy-port') options.wsProxyPort = Number(argv[++i])
  }

  return options
}

const mimeTypes = {
  '.css': 'text/css; charset=utf-8',
  '.html': 'text/html; charset=utf-8',
  '.ico': 'image/x-icon',
  '.js': 'text/javascript; charset=utf-8',
  '.json': 'application/json; charset=utf-8',
  '.png': 'image/png',
  '.svg': 'image/svg+xml',
  '.woff': 'font/woff',
  '.woff2': 'font/woff2',
}

async function readFileOrNull(filePath) {
  try {
    return await fs.readFile(filePath)
  } catch {
    return null
  }
}

const {
  host,
  port,
  root,
  wsProxyHost,
  wsProxyPort,
} = parseArgs(process.argv.slice(2))

const server = createServer(async (req, res) => {
  const url = new URL(req.url ?? '/', `http://${req.headers.host ?? `${host}:${port}`}`)
  const requestPath = decodeURIComponent(url.pathname)

  if (requestPath.startsWith('/api/')) {
    if (!wsProxyHost || !wsProxyPort) {
      res.writeHead(404)
      res.end('Not Found')
      return
    }

    const upstream = http.request({
      host: wsProxyHost,
      port: wsProxyPort,
      method: req.method,
      path: req.url,
      headers: {
        ...req.headers,
        'x-forwarded-proto': 'http',
      },
    }, (upstreamRes) => {
      res.writeHead(upstreamRes.statusCode ?? 502, upstreamRes.headers)
      upstreamRes.pipe(res)
    })

    upstream.on('error', () => {
      if (!res.headersSent) {
        res.writeHead(502)
      }
      res.end('Bad Gateway')
    })

    req.pipe(upstream)
    return
  }

  const candidate = path.join(root, requestPath)
  const safeCandidate = path.resolve(candidate)

  if (!safeCandidate.startsWith(root)) {
    res.writeHead(403)
    res.end('Forbidden')
    return
  }

  let filePath = safeCandidate
  let body = await readFileOrNull(filePath)
  if (!body && requestPath !== '/') {
    filePath = path.join(root, 'index.html')
    body = await readFileOrNull(filePath)
  }
  if (!body && requestPath === '/') {
    filePath = path.join(root, 'index.html')
    body = await readFileOrNull(filePath)
  }

  if (!body) {
    res.writeHead(404)
    res.end('Not Found')
    return
  }

  const ext = path.extname(filePath)
  res.writeHead(200, {
    'Content-Type': mimeTypes[ext] || 'application/octet-stream',
    'Cache-Control': 'no-store',
  })
  res.end(body)
})

server.on('upgrade', (req, socket, head) => {
  const url = new URL(req.url ?? '/', `http://${req.headers.host ?? `${host}:${port}`}`)
  if (url.pathname !== '/ws' || !wsProxyHost || !wsProxyPort) {
    socket.write('HTTP/1.1 404 Not Found\r\n\r\n')
    socket.destroy()
    return
  }

  const upstream = net.connect(wsProxyPort, wsProxyHost, () => {
    upstream.write(`${req.method} ${req.url} HTTP/${req.httpVersion}\r\n`)
    for (let i = 0; i < req.rawHeaders.length; i += 2) {
      upstream.write(`${req.rawHeaders[i]}: ${req.rawHeaders[i + 1]}\r\n`)
    }
    upstream.write('\r\n')
    if (head.length > 0) {
      upstream.write(head)
    }
    socket.pipe(upstream)
    upstream.pipe(socket)
  })

  upstream.on('error', () => {
    socket.destroy()
  })
  socket.on('error', () => {
    upstream.destroy()
  })
})

server.listen(port, host, () => {
  console.log(`Serving ${root} on http://${host}:${port}`)
})
