import type { Express } from 'express'
import { existsSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const resolveMobileUiRootDir = (): string => {
  const candidateDirs: string[] = []

  try {
    candidateDirs.push(fileURLToPath(new URL('../ui/mobile', import.meta.url)))
  } catch {
    // ignore and continue fallback resolution
  }

  const electronDirFromRuntime = path.resolve(path.dirname(fileURLToPath(import.meta.url)))
  candidateDirs.push(path.resolve(electronDirFromRuntime, 'headlessServer', 'ui', 'mobile'))

  candidateDirs.push(
    path.resolve(process.cwd(), 'electron', 'headlessServer', 'ui', 'mobile'),
    path.resolve(process.cwd(), 'headlessServer', 'ui', 'mobile'),
    path.resolve(process.cwd(), 'ui', 'mobile')
  )

  for (const candidateDir of candidateDirs) {
    if (existsSync(path.join(candidateDir, 'index.html'))) {
      return candidateDir
    }
  }

  throw new Error(`Unable to locate mobile UI root directory. Checked: ${candidateDirs.join(', ')}`)
}

const MOBILE_UI_ROOT = resolveMobileUiRootDir()
const MOBILE_UI_INDEX = path.join(MOBILE_UI_ROOT, 'index.html')
const MOBILE_UI_ASSETS = path.join(MOBILE_UI_ROOT, 'assets')

export function registerMobileUiRoutes(app: Express): void {
  app.get('/mobile', (_req, res) => {
    res.setHeader('Cache-Control', 'no-store')
    res.sendFile(MOBILE_UI_INDEX)
  })

  app.get('/mobile/', (_req, res) => {
    res.setHeader('Cache-Control', 'no-store')
    res.sendFile(MOBILE_UI_INDEX)
  })

  app.get('/mobile/assets/*', (req, res) => {
    const requestedPath = String(req.params[0] || '').trim()
    if (!requestedPath) {
      res.status(404).json({ success: false, error: 'Asset not found' })
      return
    }

    const normalizedPath = path.normalize(requestedPath)
    const absoluteAssetsRoot = path.resolve(MOBILE_UI_ASSETS)
    const absoluteAssetPath = path.resolve(absoluteAssetsRoot, normalizedPath)

    if (!absoluteAssetPath.startsWith(absoluteAssetsRoot + path.sep) && absoluteAssetPath !== absoluteAssetsRoot) {
      res.status(400).json({ success: false, error: 'Invalid asset path' })
      return
    }

    if (!existsSync(absoluteAssetPath)) {
      res.status(404).json({ success: false, error: 'Asset not found' })
      return
    }

    res.setHeader('Cache-Control', 'no-store')
    res.sendFile(absoluteAssetPath)
  })
}
