import { data } from "react-router";
import type { Route } from "./+types/api.auth.register";
import { startRegistration, finishRegistration, createSession, addPasskeyStart, addPasskeyFinish, registerWithMagicLink } from "~/lib/auth.server";
import { sendWelcome, sendMagicLink } from "~/lib/email.server";
import { logger } from "~/lib/logger.server";

export async function action({ request }: Route.ActionArgs) {
  const body = await request.json();
  const { step, email, username, response, challenge, userId, termsAccepted, termsVersion } = body;
  const origin = process.env.ORIGIN ?? `http://localhost:3000`;

  // Registration steps require terms acceptance + the version the client
  // agreed to (stored for audit so we can tell which text the user saw).
  const requiresTerms = step === "start" || step === "finish" || step === "register-magic-link";
  if (requiresTerms && !termsAccepted) {
    return data({ error: "Terms of Service must be accepted" }, { status: 400 });
  }
  if (requiresTerms && (typeof termsVersion !== "string" || termsVersion.length === 0)) {
    return data({ error: "Terms of Service version missing" }, { status: 400 });
  }

  try {
    if (step === "start") {
      const result = await startRegistration(email, username);
      return data({ step: "challenge", options: result.options, userId: result.userId });
    }

    if (step === "finish") {
      const newUserId = await finishRegistration(userId, email, username, response, challenge, termsVersion);
      const cookie = await createSession(newUserId, request);
      sendWelcome(email, username).catch((err) =>
        logger.error({ err }, "Failed to send welcome email"),
      );
      return data({ step: "done" }, { headers: { "Set-Cookie": cookie } });
    }

    if (step === "register-magic-link") {
      const { token, code } = await registerWithMagicLink(email, username, termsVersion);
      const link = `${origin}/auth/verify?token=${token}`;
      if (process.env.NODE_ENV !== "production") {
        // Mirror the login endpoint so devs can grab either the link or
        // the 6-digit code straight from the terminal.
        console.log(`[Register Magic Link] ${email}: ${link} (code: ${code})`);
        return data({ step: "magic-link-sent", devLink: link, code });
      }
      await sendMagicLink(email, link, code);
      sendWelcome(email, username).catch((err) =>
        logger.error({ err }, "Failed to send welcome email"),
      );
      return data({ step: "magic-link-sent" });
    }

    if (step === "add-passkey") {
      const options = await addPasskeyStart(userId);
      return data({ step: "challenge", options });
    }

    if (step === "finish-add-passkey") {
      await addPasskeyFinish(userId, response, challenge);
      return data({ step: "done" });
    }

    return data({ error: "Invalid step" }, { status: 400 });
  } catch (e) {
    return data({ error: (e as Error).message }, { status: 400 });
  }
}
