// Route protection configuration
export interface RouteConfig {
  pattern: string;
  type: "public" | "protected" | "auth";
  redirectTo?: string;
  roles?: string[]; // For future role-based access
}

export const routeConfigs: RouteConfig[] = [
  // Public routes - no authentication required
  { pattern: "/", type: "public" },
  { pattern: "/about", type: "public" },
  { pattern: "/contact", type: "public" },
  { pattern: "/help", type: "public" },
  { pattern: "/privacy", type: "public" },
  { pattern: "/terms", type: "public" },

  // Auth routes - redirect authenticated users away
  { pattern: "/auth/login", type: "auth", redirectTo: "/dashboard" },
  { pattern: "/auth/login/callback", type: "public" }, // Allow access to callback for token processing
  { pattern: "/auth/register", type: "auth", redirectTo: "/dashboard" },

  // Protected routes - require authentication
  { pattern: "/dashboard", type: "protected" },
  { pattern: "/dashboard/*", type: "protected" },
  { pattern: "/profile", type: "protected" },
  { pattern: "/settings", type: "protected" },
  { pattern: "/admin/*", type: "protected", roles: ["admin"] }, // Future: role-based
];

// Helper functions
export function matchesPattern(pathname: string, pattern: string): boolean {
  if (pattern.includes("*")) {
    const regex = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$");
    return regex.test(pathname);
  }
  return pathname === pattern || pathname.startsWith(pattern + "/");
}

export function getRouteConfig(pathname: string): RouteConfig | undefined {
  return routeConfigs.find((config) => matchesPattern(pathname, config.pattern));
}

export function isProtectedRoute(pathname: string): boolean {
  return routeConfigs.some((config) => config.type === "protected" && matchesPattern(pathname, config.pattern));
}

export function isAuthRoute(pathname: string): boolean {
  return routeConfigs.some((config) => config.type === "auth" && matchesPattern(pathname, config.pattern));
}

export function isPublicRoute(pathname: string): boolean {
  return routeConfigs.some((config) => config.type === "public" && matchesPattern(pathname, config.pattern));
}
