// Enhanced PartyKit server with session renewal support
export default class SessionValidatedPlayHTML implements PartyKitServer {
  private validSessions = new Map<string, ValidatedSession>();
  private pendingChallenges = new Map<string, SessionChallenge>();
  private usedNonces = new Set<string>();
  // Session establishment with renewal support
  async handleSessionEstablishment(request: Request): Response<Response> {
    const { challenge, signature, publicKey } = await request.json();
    // Validate challenge exists and signature is correct (ONLY crypto verification)
    const storedChallenge = this.pendingChallenges.get(challenge.challenge);
    if (!storedChallenge || storedChallenge.expiresAt < Date.now()) {
      return new Response('Invalid or expired challenge', { status: 400 });
    }
    const isValidSignature = await verifySignature(
      JSON.stringify(challenge),
      signature,
      publicKey
    );
    if (!isValidSignature) {
      return new Response('Invalid signature', { status: 400 });
    }
    // Check if this is a renewal (user already has active session)
    const existingSession = this.findExistingSession(publicKey);
    if (existingSession) {
      // Extend existing session instead of creating new one
      existingSession.expiresAt = Date.now() + (24 * 60 * 60 * 1000);
      console.log(`🔄 Renewed session for ${publicKey}`);
      return new Response(JSON.stringify({
        sessionId: existingSession.sessionId, // Keep same session ID
        publicKey: existingSession.publicKey,
        expiresAt: existingSession.expiresAt,
        renewed: true
      }));
    } else {
      // Create new session
      const session: ValidatedSession = {
        sessionId: crypto.randomUUID(),
        publicKey,