import { createAuthClient } from "better-auth/react";
import { magicLinkClient, twoFactorClient, adminClient } from "better-auth/client/plugins";
import { ac, adminRole, userRole, superAdminRole } from "./permissions";

export const authClient = createAuthClient({
  baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "",
  plugins: [
    // Magic link client
    magicLinkClient(),

    // Two-factor authentication client
    twoFactorClient({
      onTwoFactorRedirect() {
        window.location.href = "/auth/2fa";
      },
    }),

    // Admin client with RBAC
    adminClient({
      ac,
      roles: {
        "super-admin": superAdminRole,
        admin: adminRole,
        user: userRole,
      },
    }),
  ],
});

// Export commonly used hooks and methods
export const {
  signIn,
  signOut,
  useSession,
  getSession,
} = authClient;

// Type exports
export type Session = typeof authClient.$Infer.Session;
export type User = typeof authClient.$Infer.Session.user;
