import { NextResponse } from "next/server";

const DEFAULT_LOCALE = "fa"; // fallback امن

export function middleware(request) {
  const { pathname } = request.nextUrl;

  if (
    pathname.startsWith("/_next") ||
    pathname.startsWith("/api") ||
    pathname.match(/\.(.*)$/)
  ) {
    return NextResponse.next();
  }

  const segments = pathname.split("/");
  const maybeLang = segments[1];

  // فقط چک کن آیا prefix داره یا نه
  if (!maybeLang) {
    return NextResponse.redirect(
      new URL(`/${DEFAULT_LOCALE}${pathname}`, request.url)
    );
  }

  return NextResponse.next();
}

export const config = {
  matcher: ["/((?!_next|.*\\..*).*)"],
};