import type { NextRequest } from "next/server"
import { NextResponse } from "next/server"
import { ConvexHttpClient } from "convex/browser"

import { api } from "@/convex/_generated/api"
import type { Id } from "@/convex/_generated/dataModel"
import { env } from "@/lib/env"
import { assertAuthenticatedSession } from "@/lib/auth-server"
import { DEFAULT_TENANT_ID } from "@/lib/constants"
import { buildMachinesInventoryWorkbook, type MachineInventoryRecord } from "@/server/machines/inventory-export"

export const runtime = "nodejs"

type Ctx = RouteContext<"/api/admin/devices/[id]/inventory.xlsx">

function sanitizeFilename(hostname: string, fallback: string): string {
  const safe = hostname.replace(/[^a-z0-9_-]/gi, "-").replace(/-{2,}/g, "-").toLowerCase()
  return safe || fallback
}

export async function GET(request: NextRequest, ctx: Ctx) {
  const session = await assertAuthenticatedSession()
  if (!session) return NextResponse.json({ error: "Não autorizado" }, { status: 401 })

  const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
  if (!convexUrl) {
    return NextResponse.json({ error: "Convex não configurado" }, { status: 500 })
  }

  const { id } = await ctx.params
  const machineId = id as Id<"machines">
  const client = new ConvexHttpClient(convexUrl)
  const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID

  try {
    const machine = (await client.query(api.devices.getById, {
      id: machineId,
      includeMetadata: true,
    })) as MachineInventoryRecord | null

    if (!machine || machine.tenantId !== tenantId) {
      return NextResponse.json({ error: "Dispositivo não encontrada" }, { status: 404 })
    }

    const workbook = buildMachinesInventoryWorkbook([machine], {
      tenantId,
      generatedBy: session.user.name ?? session.user.email,
      companyFilterLabel: machine.companyName ?? machine.companySlug ?? null,
      generatedAt: new Date(),
    })

    const hostnameSafe = sanitizeFilename(machine.hostname, "machine")
    const body = new Uint8Array(workbook)

    return new NextResponse(body, {
      headers: {
        "Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "Content-Disposition": `attachment; filename="machine-inventory-${hostnameSafe}.xlsx"`,
        "Cache-Control": "no-store",
      },
    })
  } catch (error) {
    console.error("Failed to export machine inventory", error)
    return NextResponse.json({ error: "Falha ao gerar planilha da dispositivo" }, { status: 500 })
  }
}
