import http from "http"

const AI_SERVICE = process.env.AI_SERVICE_URL || "http://localhost:3001"

function parseUrl(url: string) {
  const u = new URL(url)
  return { hostname: u.hostname, port: parseInt(u.port) || 80, path: u.pathname + u.search }
}

export async function chatWithAI(message: string, jwtToken: string) {
  const body = JSON.stringify({ message })
  const { hostname, port, path } = parseUrl(`${AI_SERVICE}/ai/chat`)

  const text = await new Promise<string>((resolve, reject) => {
    const req = http.request(
      { hostname, port, path, method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${jwtToken}`, "Content-Length": Buffer.byteLength(body) } },
      (res) => {
        let data = ""
        res.on("data", (chunk) => data += chunk)
        res.on("end", () => {
          if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
            resolve(data)
          } else {
            reject(new Error(`AI error ${res.statusCode}: ${data.substring(0, 200)}`))
          }
        })
      }
    )
    req.on("error", reject)
    req.write(body)
    req.end()
  })

  const data = JSON.parse(text)
  return data.response || ""
}

export async function checkAiServiceStatus() {
  const { hostname, port, path } = parseUrl(`${AI_SERVICE}/health`)
  try {
    await new Promise<void>((resolve, reject) => {
      const req = http.get({ hostname, port, path, timeout: 3000 }, (res) => {
        let data = ""
        res.on("data", (chunk) => data += chunk)
        res.on("end", () => {
          try { resolve(JSON.parse(data).status === "ok" ? undefined : reject(new Error("not ok"))) } catch { reject(new Error("bad response")) }
        })
      })
      req.on("error", reject)
      req.on("timeout", () => { req.destroy(); reject(new Error("timeout")) })
    })
    return true
  } catch {
    return false
  }
}

export async function fetchJobs() {
  const { hostname, port, path } = parseUrl(`${AI_SERVICE}/ai/jobs`)
  try {
    const text = await new Promise<string>((resolve, reject) => {
      const req = http.get({ hostname, port, path, timeout: 5000 }, (res) => {
        let data = ""
        res.on("data", (chunk) => data += chunk)
        res.on("end", () => resolve(data))
      })
      req.on("error", reject)
      req.on("timeout", () => { req.destroy(); reject(new Error("timeout")) })
    })
    const data = JSON.parse(text)
    return data.jobs || []
  } catch {
    console.warn("Failed to fetch AI jobs")
    return []
  }
}
