import { auth } from '@clerk/nextjs/server';
import jwt from 'jsonwebtoken';

export async function GET() {
  try {
    // Create a temporary token that expires in 15 minutes
    const { userId } = await auth();

    if (!userId) {
      return new Response(
        JSON.stringify({ error: 'Unauthorized - Please sign in' }),
        {
          status: 401,
          headers: {
            'Content-Type': 'application/json',
          },
        }
      );
    }
    const token = jwt.sign(
      {
        deepgram_key: process.env.DEEPGRAM_API_KEY,
        iat: Math.floor(Date.now() / 1000),
        exp: Math.floor(Date.now() / 1000) + 60 * 15,
      },
      process.env.JWT_SECRET!
    );
    return Response.json({ token });
  } catch (error) {
    return new Response(JSON.stringify({ error: 'Failed to generate token' }), {
      status: 500,
      headers: {
        'Content-Type': 'application/json',
      },
    });
  }
}
