import fs from "node:fs";
import path from "node:path";
import multer from "multer";
import { uploadsRoot } from "../services/uploadPaths.js";

const productUploadDir = path.join(uploadsRoot, "products");
const shapesUploadDir = path.join(uploadsRoot, "shapes");
const brandsUploadDir = path.join(uploadsRoot, "brands");
const productTypesUploadDir = path.join(uploadsRoot, "product-types");
const reviewsUploadDir = path.join(uploadsRoot, "reviews");
const prescriptionsUploadDir = path.join(uploadsRoot, "prescriptions");
const bannersUploadDir = path.join(uploadsRoot, "banners");
fs.mkdirSync(productUploadDir, { recursive: true });
fs.mkdirSync(shapesUploadDir, { recursive: true });
fs.mkdirSync(brandsUploadDir, { recursive: true });
fs.mkdirSync(productTypesUploadDir, { recursive: true });
fs.mkdirSync(reviewsUploadDir, { recursive: true });
fs.mkdirSync(prescriptionsUploadDir, { recursive: true });
fs.mkdirSync(bannersUploadDir, { recursive: true });

const storage = multer.diskStorage({
  destination: (_req, file, cb) => {
    cb(null, productUploadDir);
  },
  filename: (_req, file, cb) => {
    const extension = path.extname(file.originalname).toLowerCase();
    const safeBaseName = path
      .basename(file.originalname, extension)
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .slice(0, 80);
    cb(null, `${Date.now()}-${safeBaseName || "product"}${extension}`);
  },
});

const shapesStorage = multer.diskStorage({
  destination: (_req, file, cb) => {
    cb(null, shapesUploadDir);
  },
  filename: (_req, file, cb) => {
    const extension = path.extname(file.originalname).toLowerCase();
    const safeBaseName = path
      .basename(file.originalname, extension)
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .slice(0, 80);
    cb(null, `${Date.now()}-${safeBaseName || "shape"}${extension}`);
  },
});

const brandsStorage = multer.diskStorage({
  destination: (_req, file, cb) => {
    cb(null, brandsUploadDir);
  },
  filename: (_req, file, cb) => {
    const extension = path.extname(file.originalname).toLowerCase();
    const safeBaseName = path
      .basename(file.originalname, extension)
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .slice(0, 80);
    cb(null, `${Date.now()}-${safeBaseName || "brand"}${extension}`);
  },
});

const productTypesStorage = multer.diskStorage({
  destination: (_req, file, cb) => {
    cb(null, productTypesUploadDir);
  },
  filename: (_req, file, cb) => {
    const extension = path.extname(file.originalname).toLowerCase();
    const safeBaseName = path
      .basename(file.originalname, extension)
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .slice(0, 80);
    cb(null, `${Date.now()}-${safeBaseName || "product-type"}${extension}`);
  },
});

export const productImageUpload = multer({
  storage,
  limits: {
    fileSize: 5 * 1024 * 1024,
    files: 8,
  },
  fileFilter: (_req, file, cb) => {
    if (!file.mimetype.startsWith("image/")) {
      cb(new Error("Only image files are allowed."));
      return;
    }
    cb(null, true);
  },
});

export const shapeImageUpload = multer({
  storage: shapesStorage,
  limits: {
    fileSize: 5 * 1024 * 1024,
    files: 4,
  },
  fileFilter: (_req, file, cb) => {
    if (!file.mimetype.startsWith("image/")) {
      cb(new Error("Only image files are allowed."));
      return;
    }
    cb(null, true);
  },
});

export const brandImageUpload = multer({
  storage: brandsStorage,
  limits: {
    fileSize: 5 * 1024 * 1024,
    files: 4,
  },
  fileFilter: (_req, file, cb) => {
    if (!file.mimetype.startsWith("image/")) {
      cb(new Error("Only image files are allowed."));
      return;
    }
    cb(null, true);
  },
});

const reviewsStorage = multer.diskStorage({
  destination: (_req, file, cb) => {
    cb(null, reviewsUploadDir);
  },
  filename: (_req, file, cb) => {
    const extension = path.extname(file.originalname).toLowerCase();
    const safeBaseName = path
      .basename(file.originalname, extension)
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .slice(0, 80);
    cb(null, `${Date.now()}-${safeBaseName || "review"}${extension}`);
  },
});

export const reviewImageUpload = multer({
  storage: reviewsStorage,
  limits: {
    fileSize: 5 * 1024 * 1024,
    files: 1,
  },
  fileFilter: (_req, file, cb) => {
    if (!file.mimetype.startsWith("image/")) {
      cb(new Error("Only image files are allowed."));
      return;
    }
    cb(null, true);
  },
});

const bannersStorage = multer.diskStorage({
  destination: (_req, file, cb) => {
    cb(null, bannersUploadDir);
  },
  filename: (_req, file, cb) => {
    const extension = path.extname(file.originalname).toLowerCase();
    const safeBaseName = path
      .basename(file.originalname, extension)
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .slice(0, 80);
    cb(null, `${Date.now()}-${safeBaseName || "banner"}${extension}`);
  },
});

export const bannerImageUpload = multer({
  storage: bannersStorage,
  limits: {
    fileSize: 5 * 1024 * 1024,
    files: 1,
  },
  fileFilter: (_req, file, cb) => {
    if (!file.mimetype.startsWith("image/")) {
      cb(new Error("Only image files are allowed."));
      return;
    }
    cb(null, true);
  },
});

export const productTypeImageUpload = multer({
  storage: productTypesStorage,
  limits: {
    fileSize: 5 * 1024 * 1024,
    files: 4,
  },
  fileFilter: (_req, file, cb) => {
    if (!file.mimetype.startsWith("image/")) {
      cb(new Error("Only image files are allowed."));
      return;
    }
    cb(null, true);
  },
});

const prescriptionsStorage = multer.diskStorage({
  destination: (_req, file, cb) => {
    cb(null, prescriptionsUploadDir);
  },
  filename: (_req, file, cb) => {
    const extension = path.extname(file.originalname).toLowerCase();
    const safeBaseName = path
      .basename(file.originalname, extension)
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .slice(0, 80);
    cb(null, `${Date.now()}-${safeBaseName || "prescription"}${extension}`);
  },
});

export const prescriptionUpload = multer({
  storage: prescriptionsStorage,
  limits: {
    fileSize: 5 * 1024 * 1024,
    files: 1,
  },
  fileFilter: (_req, file, cb) => {
    const allowed = ["image/", "application/pdf"];
    if (!allowed.some((prefix) => file.mimetype.startsWith(prefix))) {
      cb(new Error("Only image or PDF files are allowed."));
      return;
    }
    cb(null, true);
  },
});
