/**
 * DevKit Middleware
 * 
 * Access control middleware for DevKit API endpoints.
 * Only allows access when:
 * - DEVKIT_ENABLED=true
 * - NODE_ENV !== "production"
 */

import { FastifyRequest, FastifyReply } from 'fastify';
import { env } from '@4runr/shared';

/**
 * Check if DevKit is enabled
 */
export function isDevKitEnabled(): boolean {
  const devkitEnabled = env.DEVKIT_ENABLED;
  const notProduction = env.NODE_ENV !== 'production';
  
  return devkitEnabled && notProduction;
}

/**
 * DevKit access control middleware
 * Returns 403/404 if DevKit is not enabled
 */
export async function requireDevKit(
  request: FastifyRequest,
  reply: FastifyReply
): Promise<void> {
  if (!isDevKitEnabled()) {
    // Return 404 to hide the existence of DevKit endpoints in production
    reply.status(404).send({
      error: 'not_found',
      message: 'Route not found'
    });
    return;
  }
  
  // DevKit is enabled, continue to route handler
}

/**
 * Get DevKit status info
 */
export function getDevKitStatus() {
  return {
    enabled: isDevKitEnabled(),
    env: env.NODE_ENV,
    devMode: env.DEV_MODE
  };
}

