import { NextResponse } from "next/server";
import { JoseKey } from "@atproto/oauth-client-node";

const PRIVATE_KEY_ES256 = process.env.PRIVATE_KEY_ES256;

export async function GET() {
  if (!PRIVATE_KEY_ES256) {
    return NextResponse.json({ error: "PRIVATE_KEY_ES256 not configured" }, { status: 500 });
  }

  try {
    // Parse the private key and extract public JWK - no database needed
    const jwk = JSON.parse(PRIVATE_KEY_ES256);
    const key = await JoseKey.fromJWK(jwk);
    const publicJwk = key.publicJwk;

    return NextResponse.json(
      { keys: [publicJwk] },
      {
        headers: {
          "Cache-Control": "public, max-age=300",
        },
      },
    );
  } catch (err) {
    console.error("Failed to get JWKS:", err);
    return NextResponse.json({ error: "Failed to get JWKS" }, { status: 500 });
  }
}
