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

const IMAGE_TYPES = ["image/jpeg", "image/png", "image/webp", "image/avif"];
const VIDEO_TYPES = ["video/mp4", "video/webm"];
const IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".webp", ".avif"];
const VIDEO_EXTENSIONS = [".mp4", ".webm"];
const IMAGE_FOLDERS = ["team", "master-classes", "news", "classes"];
const VIDEO_FOLDERS = ["hero"];
const ALL_FOLDERS = [...IMAGE_FOLDERS, ...VIDEO_FOLDERS];
const IMAGE_MAX_SIZE = 5 * 1024 * 1024; // 5MB
const VIDEO_MAX_SIZE = 50 * 1024 * 1024; // 50MB

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

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

  const allowedTypes = isVideoFolder ? VIDEO_TYPES : IMAGE_TYPES;
  if (!allowedTypes.includes(file.type)) {
    const msg = isVideoFolder
      ? "Only MP4 and WebM videos are allowed"
      : "Only JPEG, PNG, WebP, and AVIF are allowed";
    return NextResponse.json({ error: msg }, { status: 400 });
  }

  const maxSize = isVideoFolder ? VIDEO_MAX_SIZE : IMAGE_MAX_SIZE;
  if (file.size > maxSize) {
    const label = isVideoFolder ? "50MB" : "5MB";
    return NextResponse.json(
      { error: `File too large (max ${label})` },
      { status: 400 }
    );
  }

  // Validate and sanitize filename
  const ext = path.extname(file.name).toLowerCase() || (isVideoFolder ? ".mp4" : ".webp");
  const allowedExts = isVideoFolder ? VIDEO_EXTENSIONS : IMAGE_EXTENSIONS;
  if (!allowedExts.includes(ext)) {
    return NextResponse.json(
      { error: "Invalid file extension" },
      { status: 400 }
    );
  }
  const baseName = file.name
    .replace(ext, "")
    .toLowerCase()
    .replace(/[^a-z0-9а-яё-]/gi, "-")
    .replace(/-+/g, "-")
    .slice(0, 50);
  const fileName = `${baseName}-${Date.now()}${ext}`;

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

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

  const publicPath = `/${subDir.replace(/\\/g, "/")}/${fileName}`;
  return NextResponse.json({ path: publicPath });
}
