import { NextRequest, NextResponse } from "next/server";
import { writeFile, mkdir } from "fs/promises";
import path from "path";

const ALLOWED_TYPES = ["image/jpeg", "image/png", "image/webp", "image/avif"];
const MAX_SIZE = 5 * 1024 * 1024; // 5MB

export async function POST(request: NextRequest) {
  const formData = await request.formData();
  const file = formData.get("file") as File | null;
  const folder = (formData.get("folder") as string) || "team";

  if (!file) {
    return NextResponse.json({ error: "No file provided" }, { status: 400 });
  }

  if (!ALLOWED_TYPES.includes(file.type)) {
    return NextResponse.json(
      { error: "Only JPEG, PNG, WebP, and AVIF are allowed" },
      { status: 400 }
    );
  }

  if (file.size > MAX_SIZE) {
    return NextResponse.json(
      { error: "File too large (max 5MB)" },
      { status: 400 }
    );
  }

  // Sanitize filename
  const ext = path.extname(file.name) || ".webp";
  const baseName = file.name
    .replace(ext, "")
    .toLowerCase()
    .replace(/[^a-z0-9а-яё-]/gi, "-")
    .replace(/-+/g, "-")
    .slice(0, 50);
  const fileName = `${baseName}-${Date.now()}${ext}`;

  const dir = path.join(process.cwd(), "public", "images", folder);
  await mkdir(dir, { recursive: true });

  const buffer = Buffer.from(await file.arrayBuffer());
  const filePath = path.join(dir, fileName);
  await writeFile(filePath, buffer);

  const publicPath = `/images/${folder}/${fileName}`;
  return NextResponse.json({ path: publicPath });
}
