"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadPrescription = exports.uploadBannerImages = exports.uploadProductTypeImages = exports.uploadReviewImages = exports.uploadBrandImages = exports.uploadShapeImages = exports.uploadProductImages = void 0;
const node_path_1 = __importDefault(require("node:path"));
const uploadPaths_js_1 = require("../services/uploadPaths.js");
const apiResponse_js_1 = require("../utils/apiResponse.js");
const uploadProductImages = async (req, res) => {
    const files = req.files;
    if (!files?.length) {
        return (0, apiResponse_js_1.sendError)(res, 400, "NO_FILES", "No image files were uploaded.");
    }
    const media = files.map((file, index) => {
        const storageKey = node_path_1.default.posix.join("products", file.filename);
        return {
            url: (0, uploadPaths_js_1.publicUploadUrl)(storageKey),
            storageKey,
            fileName: file.originalname,
            mimeType: file.mimetype,
            sizeBytes: file.size,
            altText: null,
            sortOrder: index,
            isPrimary: index === 0,
        };
    });
    return (0, apiResponse_js_1.sendSuccess)(res, 201, { media }, "Images uploaded.");
};
exports.uploadProductImages = uploadProductImages;
const uploadShapeImages = async (req, res) => {
    const files = req.files;
    if (!files?.length) {
        return (0, apiResponse_js_1.sendError)(res, 400, "NO_FILES", "No image files were uploaded.");
    }
    const media = files.map((file, index) => {
        const storageKey = node_path_1.default.posix.join("shapes", file.filename);
        return {
            url: (0, uploadPaths_js_1.publicUploadUrl)(storageKey),
            storageKey,
            fileName: file.originalname,
            mimeType: file.mimetype,
            sizeBytes: file.size,
            altText: null,
            sortOrder: index,
            isPrimary: index === 0,
        };
    });
    return (0, apiResponse_js_1.sendSuccess)(res, 201, { media }, "Images uploaded.");
};
exports.uploadShapeImages = uploadShapeImages;
const uploadBrandImages = async (req, res) => {
    const files = req.files;
    if (!files?.length) {
        return (0, apiResponse_js_1.sendError)(res, 400, "NO_FILES", "No image files were uploaded.");
    }
    const media = files.map((file, index) => {
        const storageKey = node_path_1.default.posix.join("brands", file.filename);
        return {
            url: (0, uploadPaths_js_1.publicUploadUrl)(storageKey),
            storageKey,
            fileName: file.originalname,
            mimeType: file.mimetype,
            sizeBytes: file.size,
            altText: null,
            sortOrder: index,
            isPrimary: index === 0,
        };
    });
    return (0, apiResponse_js_1.sendSuccess)(res, 201, { media }, "Images uploaded.");
};
exports.uploadBrandImages = uploadBrandImages;
const uploadReviewImages = async (req, res) => {
    const files = req.files;
    if (!files?.length) {
        return (0, apiResponse_js_1.sendError)(res, 400, "NO_FILES", "No image files were uploaded.");
    }
    const media = files.map((file, index) => {
        const storageKey = node_path_1.default.posix.join("reviews", file.filename);
        return {
            url: (0, uploadPaths_js_1.publicUploadUrl)(storageKey),
            storageKey,
            fileName: file.originalname,
            mimeType: file.mimetype,
            sizeBytes: file.size,
            altText: null,
            sortOrder: index,
            isPrimary: index === 0,
        };
    });
    return (0, apiResponse_js_1.sendSuccess)(res, 201, { media }, "Images uploaded.");
};
exports.uploadReviewImages = uploadReviewImages;
const uploadProductTypeImages = async (req, res) => {
    const files = req.files;
    if (!files?.length) {
        return (0, apiResponse_js_1.sendError)(res, 400, "NO_FILES", "No image files were uploaded.");
    }
    const media = files.map((file, index) => {
        const storageKey = node_path_1.default.posix.join("product-types", file.filename);
        return {
            url: (0, uploadPaths_js_1.publicUploadUrl)(storageKey),
            storageKey,
            fileName: file.originalname,
            mimeType: file.mimetype,
            sizeBytes: file.size,
            altText: null,
            sortOrder: index,
            isPrimary: index === 0,
        };
    });
    return (0, apiResponse_js_1.sendSuccess)(res, 201, { media }, "Images uploaded.");
};
exports.uploadProductTypeImages = uploadProductTypeImages;
const uploadBannerImages = async (req, res) => {
    const files = req.files;
    if (!files?.length) {
        return (0, apiResponse_js_1.sendError)(res, 400, "NO_FILES", "No image files were uploaded.");
    }
    const file = files[0];
    const storageKey = node_path_1.default.posix.join("banners", file.filename);
    return (0, apiResponse_js_1.sendSuccess)(res, 201, {
        url: (0, uploadPaths_js_1.publicUploadUrl)(storageKey),
        storageKey,
        fileName: file.originalname,
        mimeType: file.mimetype,
        sizeBytes: file.size,
    }, "Banner image uploaded.");
};
exports.uploadBannerImages = uploadBannerImages;
const uploadPrescription = async (req, res) => {
    const files = req.files;
    if (!files?.length) {
        return (0, apiResponse_js_1.sendError)(res, 400, "NO_FILES", "No prescription file was uploaded.");
    }
    const file = files[0];
    const storageKey = node_path_1.default.posix.join("prescriptions", file.filename);
    return (0, apiResponse_js_1.sendSuccess)(res, 201, {
        url: (0, uploadPaths_js_1.publicUploadUrl)(storageKey),
        storageKey,
        fileName: file.originalname,
        mimeType: file.mimetype,
        sizeBytes: file.size,
    }, "Prescription uploaded.");
};
exports.uploadPrescription = uploadPrescription;
//# sourceMappingURL=uploadController.js.map