import type { NextAuthConfig } from "next-auth";

/**
 * Auth.js v5 config -- edge-safe (no Node.js-only imports).
 * Used by proxy.ts (middleware). The full auth.ts extends this with
 * the Credentials provider that needs bcryptjs + Prisma.
 */
export const authConfig = {
  pages: {
    signIn: "/auth/login",
  },
  session: {
    strategy: "jwt",
    maxAge: 30 * 24 * 60 * 60, // 30 days
    updateAge: 24 * 60 * 60,   // Refresh token daily
  },
  providers: [], // Filled in by auth.ts
  callbacks: {
    authorized({ auth, request: { nextUrl } }) {
      const isAuthenticated = !!auth?.user;
      const isOnDashboard = nextUrl.pathname.startsWith("/dashboard");
      const isOnAuth = nextUrl.pathname.startsWith("/auth/");

      if (isOnDashboard && !isAuthenticated) {
        return false; // Redirect to signIn page
      }

      if (isOnAuth && isAuthenticated) {
        return Response.redirect(new URL("/dashboard", nextUrl.origin));
      }

      return true;
    },
  },
} satisfies NextAuthConfig;
