/**
 * Route Hierarchy Utility
 * Understands framework-specific route hierarchy conventions
 *
 * This addresses false positives where routes inside authenticated layouts
 * are flagged for "missing auth" when they inherit protection from parents.
 *
 * Key insight: Modern frameworks use layouts/middleware that protect entire
 * route subtrees - child routes inherit this protection.
 */

export interface RouteProtectionContext {
  /** Whether this route is in a protected hierarchy */
  isInProtectedHierarchy: boolean
  /** Sources of protection (e.g., ["Remix _authenticated+ layout"]) */
  protectionSource: string[]
  /** Framework detected */
  framework?: 'remix' | 'nextjs' | 'sveltekit' | 'nuxt' | 'unknown'
  /** Whether this is in an admin section */
  isAdminRoute: boolean
}

/**
 * Framework-specific route conventions
 */
const FRAMEWORK_ROUTE_CONVENTIONS = {
  remix: {
    // Pathless layout routes - children inherit from parent
    protectedLayouts: [
      { pattern: /_authenticated\+/, name: 'Remix _authenticated+ layout' },
      { pattern: /\(authenticated\)/, name: 'Remix (authenticated) route group' },
      { pattern: /_protected\+/, name: 'Remix _protected+ layout' },
      { pattern: /\(protected\)/, name: 'Remix (protected) route group' },
      { pattern: /__authenticated/, name: 'Remix __authenticated layout' },
      { pattern: /_auth\+/, name: 'Remix _auth+ layout' },
    ],
    adminLayouts: [
      { pattern: /admin\+/, name: 'Remix admin+ layout' },
      { pattern: /\(admin\)/, name: 'Remix (admin) route group' },
      { pattern: /_admin\+/, name: 'Remix _admin+ layout' },
      { pattern: /__admin/, name: 'Remix __admin layout' },
    ],
    // Detection pattern for Remix routes
    routeDetection: /routes\//,
  },

  nextjs: {
    protectedGroups: [
      { pattern: /\(authenticated\)/, name: 'Next.js (authenticated) route group' },
      { pattern: /\(protected\)/, name: 'Next.js (protected) route group' },
      { pattern: /\(dashboard\)/, name: 'Next.js (dashboard) route group' },
      { pattern: /\(private\)/, name: 'Next.js (private) route group' },
      { pattern: /\(secure\)/, name: 'Next.js (secure) route group' },
      { pattern: /\(user\)/, name: 'Next.js (user) route group' },
      { pattern: /\(account\)/, name: 'Next.js (account) route group' },
      { pattern: /\(app\)/, name: 'Next.js (app) route group' },
    ],
    adminGroups: [
      { pattern: /\(admin\)/, name: 'Next.js (admin) route group' },
    ],
    // Detection pattern for Next.js App Router
    routeDetection: /\/app\//,
  },

  sveltekit: {
    protectedGroups: [
      { pattern: /\(authenticated\)/, name: 'SvelteKit (authenticated) group' },
      { pattern: /\(protected\)/, name: 'SvelteKit (protected) group' },
      { pattern: /\(authed\)/, name: 'SvelteKit (authed) group' },
    ],
    adminGroups: [
      { pattern: /\(admin\)/, name: 'SvelteKit (admin) group' },
    ],
    routeDetection: /\/routes\//,
  },
}

/**
 * Generic protected path patterns (framework-agnostic)
 */
const GENERIC_PROTECTED_PATTERNS = [
  // Dashboard sections typically require auth
  { pattern: /\/dashboard\//, name: 'Dashboard section' },
  // Account/settings sections
  { pattern: /\/account\//, name: 'Account section' },
  { pattern: /\/settings\//, name: 'Settings section' },
  { pattern: /\/profile\//, name: 'Profile section' },
  // Admin sections
  { pattern: /\/admin\//, name: 'Admin section' },
  // User-specific routes with dynamic segments
  { pattern: /\/users\/\[/, name: 'User-specific route' },
  { pattern: /\/user\/\[/, name: 'User-specific route' },
  // My-* routes (personal content)
  { pattern: /\/my-/, name: 'Personal content route' },
]

/**
 * Admin path patterns
 */
const ADMIN_PATH_PATTERNS = [
  /\/admin\//i,
  /\(admin\)/i,
  /_admin\+/i,
  /admin\+/i,
  /\/backoffice\//i,
  /\/management\//i,
]

/**
 * Detect which framework this file belongs to
 */
function detectFramework(filePath: string): RouteProtectionContext['framework'] {
  // Check for Remix patterns
  if (FRAMEWORK_ROUTE_CONVENTIONS.remix.routeDetection.test(filePath)) {
    // Remix uses routes/ directory
    if (/_authenticated\+|_\w+\+/.test(filePath)) {
      return 'remix'
    }
  }

  // Check for Next.js App Router patterns
  if (FRAMEWORK_ROUTE_CONVENTIONS.nextjs.routeDetection.test(filePath)) {
    return 'nextjs'
  }

  // Check for SvelteKit patterns
  if (FRAMEWORK_ROUTE_CONVENTIONS.sveltekit.routeDetection.test(filePath)) {
    return 'sveltekit'
  }

  return 'unknown'
}

/**
 * Get route protection context for a file path
 *
 * @param filePath - The full file path to analyze
 * @returns RouteProtectionContext with protection information
 */
export function getRouteProtectionContext(filePath: string): RouteProtectionContext {
  const sources: string[] = []
  const framework = detectFramework(filePath)
  let isAdminRoute = false

  // Check Remix conventions
  for (const layout of FRAMEWORK_ROUTE_CONVENTIONS.remix.protectedLayouts) {
    if (layout.pattern.test(filePath)) {
      sources.push(layout.name)
    }
  }
  for (const layout of FRAMEWORK_ROUTE_CONVENTIONS.remix.adminLayouts) {
    if (layout.pattern.test(filePath)) {
      sources.push(layout.name)
      isAdminRoute = true
    }
  }

  // Check Next.js conventions
  for (const group of FRAMEWORK_ROUTE_CONVENTIONS.nextjs.protectedGroups) {
    if (group.pattern.test(filePath)) {
      sources.push(group.name)
    }
  }
  for (const group of FRAMEWORK_ROUTE_CONVENTIONS.nextjs.adminGroups) {
    if (group.pattern.test(filePath)) {
      sources.push(group.name)
      isAdminRoute = true
    }
  }

  // Check SvelteKit conventions
  for (const group of FRAMEWORK_ROUTE_CONVENTIONS.sveltekit.protectedGroups) {
    if (group.pattern.test(filePath)) {
      sources.push(group.name)
    }
  }
  for (const group of FRAMEWORK_ROUTE_CONVENTIONS.sveltekit.adminGroups) {
    if (group.pattern.test(filePath)) {
      sources.push(group.name)
      isAdminRoute = true
    }
  }

  // Check generic protected patterns
  for (const pattern of GENERIC_PROTECTED_PATTERNS) {
    if (pattern.pattern.test(filePath)) {
      sources.push(pattern.name)
    }
  }

  // Check if this is an admin route
  if (!isAdminRoute) {
    isAdminRoute = ADMIN_PATH_PATTERNS.some(pattern => pattern.test(filePath))
  }

  return {
    isInProtectedHierarchy: sources.length > 0,
    protectionSource: sources,
    framework,
    isAdminRoute,
  }
}

/**
 * Check if a file path is inside a protected route hierarchy
 *
 * @param filePath - The full file path
 * @returns true if the route is protected by its hierarchy
 */
export function isProtectedByRouteHierarchy(filePath: string): boolean {
  const context = getRouteProtectionContext(filePath)
  return context.isInProtectedHierarchy
}

/**
 * Check if a file path is inside an admin route section
 *
 * @param filePath - The full file path
 * @returns true if this is an admin route
 */
export function isAdminRoute(filePath: string): boolean {
  const context = getRouteProtectionContext(filePath)
  return context.isAdminRoute
}

/**
 * Check if a component is likely used only within authenticated routes
 * Useful for components in /components/admin/ or similar
 *
 * @param filePath - The full file path
 * @returns true if this component is likely only used in authenticated contexts
 */
export function isAuthenticatedOnlyComponent(filePath: string): boolean {
  const authenticatedComponentPatterns = [
    /\/components\/admin\//i,
    /\/components\/dashboard\//i,
    /\/components\/user\//i,
    /\/components\/account\//i,
    /\/components\/settings\//i,
    /\/components\/protected\//i,
    /\/components\/private\//i,
    /\/components\/authenticated\//i,
    // Features that are inherently authenticated
    /\/features\/dashboard\//i,
    /\/features\/admin\//i,
    /\/features\/account\//i,
  ]

  return authenticatedComponentPatterns.some(pattern => pattern.test(filePath))
}
