// app/api/beta-signup/route.ts
import { NextResponse } from "next/server";

const API_BASE_URL =
  process.env.NEXT_PUBLIC_API_BASE_URL ?? "";

const TOS_VERSION = "2025-12-27";

type BetaSignupBody = {
  email?: string;
  useCase?: string;
  acceptedTos?: boolean;
  tosVersion?: string;
};

export async function POST(req: Request) {
  try {
    const body = (await req.json()) as BetaSignupBody;

    const email = (body.email ?? "").trim().toLowerCase();
    const useCase = (body.useCase ?? "").trim();
    const acceptedTos = Boolean(body.acceptedTos);
    const tosVersion = (body.tosVersion ?? TOS_VERSION).trim() || TOS_VERSION;

    // Keep "no enumeration": always return ok:true, but silently refuse bad input
    if (!email || !acceptedTos) {
      return NextResponse.json({ ok: true });
    }

    // Forward only known fields
    const payload = { email, useCase, acceptedTos, tosVersion };

    const res = await fetch(`${API_BASE_URL}/api/auth/beta/signup`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
      cache: "no-store",
    });

    if (!res.ok) {
      const text = await res.text().catch(() => "");
      console.error("beta-signup proxy failed:", res.status, text);
    }

    return NextResponse.json({ ok: true });
  } catch (e) {
    console.error("beta-signup proxy error:", e);
    return NextResponse.json({ ok: true });
  }
}