import { createAuthEngine, MemoryAuthStore } from "@wrnexus/auth";

export const outbox: unknown[] = [];

export const auth = createAuthEngine({
  store: new MemoryAuthStore(),
  secret: "development-auth-showcase-secret-change-in-production",
  issuer: "WRNexus Auth Showcase",
  sendLoginAlerts: true,
  onSignedIn(ctx, returnTo) {
    const safe = returnTo?.startsWith("/") && !returnTo.startsWith("//") ? returnTo : "/account";
    return Response.redirect(new URL(safe, ctx.url), 303);
  },
  onSignedOut(ctx) {
    return Response.redirect(new URL("/sign-in", ctx.url), 303);
  },
  onSuccessfulSignUp() {
    return {
      autoSignIn: true,
      redirectTo: "/account",
    };
  },
  delivery: {
    async send(message) {
      outbox.push(message);
      console.info(`[auth-showcase] queued ${message.template} for ${message.destination}`);
    },
  },
});
