import { db } from '@/lib/db'
import { machines, sessionCodes } from '@/lib/db/schema'
import { eq, and, isNotNull, gt } from 'drizzle-orm'
import { NextRequest, NextResponse } from 'next/server'

export async function POST(request: NextRequest) {
  try {
    const { accessKey } = await request.json()

    if (!accessKey) {
      return NextResponse.json({ error: 'Access key required' }, { status: 400 })
    }

    const result = await db
      .update(machines)
      .set({ isOnline: true, lastSeen: new Date() })
      .where(eq(machines.accessKey, accessKey))
      .returning({ id: machines.id })

    if (!result[0]) {
      return NextResponse.json({ error: 'Invalid access key' }, { status: 401 })
    }

    const machineId = result[0].id

    // Check for a pending connection (code used recently)
    const pending = await db
      .select()
      .from(sessionCodes)
      .where(
        and(
          eq(sessionCodes.machineId, machineId),
          eq(sessionCodes.isActive, true),
          gt(sessionCodes.expiresAt, new Date()),
          isNotNull(sessionCodes.usedAt)
        )
      )
      .orderBy(sessionCodes.usedAt)
      .limit(1)

    return NextResponse.json({
      success: true,
      pendingConnection: pending[0]
        ? { sessionCodeId: pending[0].id, usedBy: pending[0].usedBy }
        : null,
    })
  } catch (error) {
    console.error('[Heartbeat] Error:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
}
