"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv/config");
const express_1 = __importDefault(require("express"));
const multer_1 = __importDefault(require("multer"));
const cors_1 = __importDefault(require("cors"));
const helmet_1 = __importDefault(require("helmet"));
const morgan_1 = __importDefault(require("morgan"));
const authRoutes_js_1 = __importDefault(require("./routes/authRoutes.js"));
const productRoutes_js_1 = __importDefault(require("./routes/productRoutes.js"));
const orderRoutes_js_1 = __importDefault(require("./routes/orderRoutes.js"));
const publicClientReviewRoutes_js_1 = __importDefault(require("./routes/publicClientReviewRoutes.js"));
const adminProductRoutes_js_1 = __importDefault(require("./routes/adminProductRoutes.js"));
const adminOrderRoutes_js_1 = __importDefault(require("./routes/adminOrderRoutes.js"));
const adminDashboardRoutes_js_1 = __importDefault(require("./routes/adminDashboardRoutes.js"));
const adminUploadRoutes_js_1 = __importDefault(require("./routes/adminUploadRoutes.js"));
const adminFrameShapeRoutes_js_1 = __importDefault(require("./routes/adminFrameShapeRoutes.js"));
const adminBrandRoutes_js_1 = __importDefault(require("./routes/adminBrandRoutes.js"));
const adminClientReviewRoutes_js_1 = __importDefault(require("./routes/adminClientReviewRoutes.js"));
const adminBannerRoutes_js_1 = __importDefault(require("./routes/adminBannerRoutes.js"));
const publicBannerRoutes_js_1 = __importDefault(require("./routes/publicBannerRoutes.js"));
const adminProductTypeRoutes_js_1 = __importDefault(require("./routes/adminProductTypeRoutes.js"));
const uploadPaths_js_1 = require("./services/uploadPaths.js");
const apiResponse_js_1 = require("./utils/apiResponse.js");
const app = (0, express_1.default)();
const PORT = process.env.PORT || 8000;
const corsOrigins = process.env.FRONTEND_URL
    ? process.env.FRONTEND_URL.split(",").map((o) => o.trim())
    : true;
app.use((0, cors_1.default)({ origin: corsOrigins, credentials: true }));
app.use((0, helmet_1.default)({ crossOriginResourcePolicy: { policy: "cross-origin" } }));
app.use((0, morgan_1.default)("dev"));
app.use(express_1.default.json({ limit: "1mb" }));
app.use("/uploads", express_1.default.static(uploadPaths_js_1.uploadsRoot));
app.get("/api/health", (_req, res) => {
    res.json({ status: "ok" });
});
app.use("/api/auth", authRoutes_js_1.default);
app.use("/api/products", productRoutes_js_1.default);
app.use("/api/orders", orderRoutes_js_1.default);
app.use("/api/client-reviews", publicClientReviewRoutes_js_1.default);
app.use("/api/admin/products", adminProductRoutes_js_1.default);
app.use("/api/admin/orders", adminOrderRoutes_js_1.default);
app.use("/api/admin/dashboard", adminDashboardRoutes_js_1.default);
app.use("/api/admin/uploads", adminUploadRoutes_js_1.default);
app.use("/api/admin/frame-shapes", adminFrameShapeRoutes_js_1.default);
app.use("/api/admin/brands", adminBrandRoutes_js_1.default);
app.use("/api/admin/client-reviews", adminClientReviewRoutes_js_1.default);
app.use("/api/banners", publicBannerRoutes_js_1.default);
app.use("/api/admin/banners", adminBannerRoutes_js_1.default);
app.use("/api/admin/product-types", adminProductTypeRoutes_js_1.default);
app.use((_req, res) => {
    return (0, apiResponse_js_1.sendError)(res, 404, "NOT_FOUND", "Route not found.");
});
app.use((err, _req, res, _next) => {
    console.error(err);
    if (err instanceof multer_1.default.MulterError) {
        return (0, apiResponse_js_1.sendError)(res, 400, "UPLOAD_ERROR", err.message);
    }
    if (err instanceof Error &&
        err.message === "Only image files are allowed.") {
        return (0, apiResponse_js_1.sendError)(res, 400, "INVALID_FILE_TYPE", err.message);
    }
    return (0, apiResponse_js_1.sendError)(res, 500, "INTERNAL_SERVER_ERROR", "Something went wrong.");
});
app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});
//# sourceMappingURL=server.js.map