import { NextRequest, NextResponse } from "next/server";
import crypto from 'crypto';

export async function GET(req: NextRequest) {
  const room = req.nextUrl.searchParams.get("room");
  const username = req.nextUrl.searchParams.get("username");

  if (!room || !username) {
    return NextResponse.json({ error: 'Missing "room" or "username" query parameter' }, { status: 400 });
  }

  // FASE 82: THE GREAT PURGE
  // XCU Ultra has been eradicated. We now generate a pure XCU Quantum Token.
  const tokenPayload = {
     room,
     username,
     iat: Math.floor(Date.now() / 1000),
     exp: Math.floor(Date.now() / 1000) + 3600,
     matrix_id: crypto.randomUUID()
  };

  const token = Buffer.from(JSON.stringify(tokenPayload)).toString('base64');

  return NextResponse.json({
    token: `XCU_${token}`,
    engineStrategy: 'XCU_DIRECTOR'
  });
}
