/* eslint-disable @typescript-eslint/no-explicit-any */
import { fetchWithTimeout } from "@/lib/helpers/fetchWithTimeout";
import { PWA_ICON_192 } from "@/lib/helpers/pwaIcons";
import { NextRequest, NextResponse } from "next/server";
import sharp from "sharp";

export const dynamic = "force-dynamic";
export const revalidate = 0;

const FETCH_TIMEOUT_MS = 5000;
const DEFAULT_ICON = PWA_ICON_192;

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ name: string }> },
) {
  // Restaurant logo processing disabled – redirect to static PWA icon
  return NextResponse.redirect(new URL(PWA_ICON_192, request.url), 302);

  /* Restaurant logo processing – uncomment to re-enable dynamic PWA icons
  try {
    await params;

    const searchParams = request.nextUrl.searchParams;
    const logoUrl = searchParams.get("url");
    const size = parseInt(searchParams.get("size") || "192");

    if (!logoUrl) {
      return new NextResponse("Logo URL is required", { status: 400 });
    }

    const imageResponse = await fetchWithTimeout(logoUrl, {
      timeoutMs: FETCH_TIMEOUT_MS,
    });
    if (!imageResponse.ok) {
      return NextResponse.redirect(new URL(DEFAULT_ICON, request.url), 302);
    }

    const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());

    const metadata = await sharp(imageBuffer).metadata();
    const { width, height } = metadata;

    if (!width || !height) {
      return NextResponse.redirect(new URL(DEFAULT_ICON, request.url), 302);
    }

    if (width === height) {
      const processedImage = sharp(imageBuffer).resize(size, size, {
        fit: "contain",
        background: { r: 0, g: 0, b: 0, alpha: 0 },
      });
      const outputBuffer: any = await processedImage.png().toBuffer();

      return new NextResponse(outputBuffer, {
        headers: {
          "Content-Type": "image/png",
          "Cache-Control": "public, max-age=31536000, immutable",
        },
      });
    }

    const maxDimension = Math.max(width, height);
    const scale = size / maxDimension;
    const newWidth = Math.round(width * scale);
    const newHeight = Math.round(height * scale);

    const paddingTop = Math.floor((size - newHeight) / 2);
    const paddingBottom = size - newHeight - paddingTop;
    const paddingLeft = Math.floor((size - newWidth) / 2);
    const paddingRight = size - newWidth - paddingLeft;

    const processedImage = sharp(imageBuffer)
      .resize(newWidth, newHeight, {
        fit: "contain",
        background: { r: 0, g: 0, b: 0, alpha: 0 },
      })
      .extend({
        top: paddingTop,
        bottom: paddingBottom,
        left: paddingLeft,
        right: paddingRight,
        background: { r: 0, g: 0, b: 0, alpha: 0 },
      });

    const outputBuffer: any = await processedImage.png().toBuffer();

    return new NextResponse(outputBuffer, {
      headers: {
        "Content-Type": "image/png",
        "Cache-Control": "public, max-age=31536000, immutable",
      },
    });
  } catch (error) {
    console.error("Error processing logo:", error);
    return NextResponse.redirect(new URL(DEFAULT_ICON, request.url), 302);
  }
  */
}
