import path from "node:path";
import type { Response } from "express";
import type { UploadedMediaResponse } from "../@types/uploads.js";
import type { AuthenticatedRequest } from "../@types/auth.js";
import { publicUploadUrl } from "../services/uploadPaths.js";
import { sendError, sendSuccess } from "../utils/apiResponse.js";

export const uploadProductImages = async (req: AuthenticatedRequest, res: Response) => {
  const files = req.files as Express.Multer.File[] | undefined;

  if (!files?.length) {
    return sendError(res, 400, "NO_FILES", "No image files were uploaded.");
  }

  const media: UploadedMediaResponse[] = files.map((file, index) => {
    const storageKey = path.posix.join("products", file.filename);
    return {
      url: publicUploadUrl(storageKey),
      storageKey,
      fileName: file.originalname,
      mimeType: file.mimetype,
      sizeBytes: file.size,
      altText: null,
      sortOrder: index,
      isPrimary: index === 0,
    };
  });

  return sendSuccess(res, 201, { media }, "Images uploaded.");
};

export const uploadShapeImages = async (req: AuthenticatedRequest, res: Response) => {
  const files = req.files as Express.Multer.File[] | undefined;

  if (!files?.length) {
    return sendError(res, 400, "NO_FILES", "No image files were uploaded.");
  }

  const media: UploadedMediaResponse[] = files.map((file, index) => {
    const storageKey = path.posix.join("shapes", file.filename);
    return {
      url: publicUploadUrl(storageKey),
      storageKey,
      fileName: file.originalname,
      mimeType: file.mimetype,
      sizeBytes: file.size,
      altText: null,
      sortOrder: index,
      isPrimary: index === 0,
    };
  });

  return sendSuccess(res, 201, { media }, "Images uploaded.");
};

export const uploadBrandImages = async (req: AuthenticatedRequest, res: Response) => {
  const files = req.files as Express.Multer.File[] | undefined;

  if (!files?.length) {
    return sendError(res, 400, "NO_FILES", "No image files were uploaded.");
  }

  const media: UploadedMediaResponse[] = files.map((file, index) => {
    const storageKey = path.posix.join("brands", file.filename);
    return {
      url: publicUploadUrl(storageKey),
      storageKey,
      fileName: file.originalname,
      mimeType: file.mimetype,
      sizeBytes: file.size,
      altText: null,
      sortOrder: index,
      isPrimary: index === 0,
    };
  });

  return sendSuccess(res, 201, { media }, "Images uploaded.");
};

export const uploadReviewImages = async (req: AuthenticatedRequest, res: Response) => {
  const files = req.files as Express.Multer.File[] | undefined;

  if (!files?.length) {
    return sendError(res, 400, "NO_FILES", "No image files were uploaded.");
  }

  const media: UploadedMediaResponse[] = files.map((file, index) => {
    const storageKey = path.posix.join("reviews", file.filename);
    return {
      url: publicUploadUrl(storageKey),
      storageKey,
      fileName: file.originalname,
      mimeType: file.mimetype,
      sizeBytes: file.size,
      altText: null,
      sortOrder: index,
      isPrimary: index === 0,
    };
  });

  return sendSuccess(res, 201, { media }, "Images uploaded.");
};

export const uploadProductTypeImages = async (req: AuthenticatedRequest, res: Response) => {
  const files = req.files as Express.Multer.File[] | undefined;

  if (!files?.length) {
    return sendError(res, 400, "NO_FILES", "No image files were uploaded.");
  }

  const media: UploadedMediaResponse[] = files.map((file, index) => {
    const storageKey = path.posix.join("product-types", file.filename);
    return {
      url: publicUploadUrl(storageKey),
      storageKey,
      fileName: file.originalname,
      mimeType: file.mimetype,
      sizeBytes: file.size,
      altText: null,
      sortOrder: index,
      isPrimary: index === 0,
    };
  });

  return sendSuccess(res, 201, { media }, "Images uploaded.");
};

export const uploadBannerImages = async (req: AuthenticatedRequest, res: Response) => {
  const files = req.files as Express.Multer.File[] | undefined;

  if (!files?.length) {
    return sendError(res, 400, "NO_FILES", "No image files were uploaded.");
  }

  const file = files[0]!;
  const storageKey = path.posix.join("banners", file.filename);

  return sendSuccess(res, 201, {
    url: publicUploadUrl(storageKey),
    storageKey,
    fileName: file.originalname,
    mimeType: file.mimetype,
    sizeBytes: file.size,
  }, "Banner image uploaded.");
};

export const uploadPrescription = async (req: AuthenticatedRequest, res: Response) => {
  const files = req.files as Express.Multer.File[] | undefined;

  if (!files?.length) {
    return sendError(res, 400, "NO_FILES", "No prescription file was uploaded.");
  }

  const file = files[0]!;
  const storageKey = path.posix.join("prescriptions", file.filename);

  return sendSuccess(res, 201, {
    url: publicUploadUrl(storageKey),
    storageKey,
    fileName: file.originalname,
    mimeType: file.mimetype,
    sizeBytes: file.size,
  }, "Prescription uploaded.");
};
