import { defineMiddleware } from 'astro:middleware';

const BLOCKED_PATHS = new Set<string>([
  '/sql',
  '/.env',
  '/.git/config',
  '/wp-admin',
  '/wp-login.php',
  '/xmlrpc.php',
  '/phpmyadmin',
  '/.aws/credentials',
  '/server-status',
  '/actuator',
  '/actuator/env',
  '/.DS_Store',
]);

const BLOCKED_PREFIXES = ['/wp-admin/', '/wp-content/', '/phpmyadmin/', '/.git/'];

export const botProbeBlock = defineMiddleware(async (ctx, next) => {
  const p = ctx.url.pathname;
  if (BLOCKED_PATHS.has(p) || BLOCKED_PREFIXES.some((pre) => p.startsWith(pre))) {
    return new Response('Not Found', {
      status: 404,
      headers: {
        'Cache-Control': 'public, max-age=86400, s-maxage=86400',
        'Content-Type': 'text/plain; charset=utf-8',
      },
    });
  }
  return next();
});
