import { NextResponse } from "next/server"
import { z } from "zod"
import { ConvexHttpClient } from "convex/browser"

import { assertAuthenticatedSession } from "@/lib/auth-server"
import { DEFAULT_TENANT_ID } from "@/lib/constants"
import { api } from "@/convex/_generated/api"
import type { Id } from "@/convex/_generated/dataModel"
import { prisma } from "@/lib/prisma"

export const runtime = "nodejs"

const schema = z.object({
  machineId: z.string().min(1),
})

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

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

  const payload = await request.json().catch(() => null)
  const parsed = schema.safeParse(payload)
  if (!parsed.success) {
    return NextResponse.json({ error: "Payload inválido", details: parsed.error.flatten() }, { status: 400 })
  }

  const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID

  try {
    const convex = new ConvexHttpClient(convexUrl)
    const ensured = await convex.mutation(api.users.ensureUser, {
      tenantId,
      email: session.user.email,
      name: session.user.name ?? session.user.email,
      avatarUrl: session.user.avatarUrl ?? undefined,
      role: session.user.role.toUpperCase(),
    })

    const actorId = ensured?._id as Id<"users"> | undefined
    if (!actorId) {
      return NextResponse.json({ error: "Falha ao identificar o administrador" }, { status: 500 })
    }

    let machineMissing = false
    try {
      await convex.mutation(api.machines.remove, {
        machineId: parsed.data.machineId as Id<"machines">,
        actorId,
      })
    } catch (error) {
      const message = error instanceof Error ? error.message : ""
      if (message.includes("Máquina não encontrada")) {
        machineMissing = true
      } else {
        console.error("[machines.delete] Convex failure", error)
        return NextResponse.json({ error: "Falha ao remover máquina no Convex" }, { status: 500 })
      }
    }

    const machineEmail = `machine-${parsed.data.machineId}@machines.local`
    await prisma.authUser.deleteMany({ where: { email: machineEmail } })

    return NextResponse.json({ ok: true, machineMissing })
  } catch (error) {
    console.error("[machines.delete] Falha ao excluir", error)
    return NextResponse.json({ error: "Falha ao excluir máquina" }, { status: 500 })
  }
}
