"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const path_1 = __importDefault(require("path"));
const database_1 = require("./db/database");
const seed_1 = require("./db/seed");
const templates_1 = require("./routes/templates");
const creatives_1 = require("./routes/creatives");
const assets_1 = require("./routes/assets");
const builds_1 = require("./routes/builds");
const preview_1 = require("./routes/preview");
const preview_2 = require("./routes/preview");
const errorHandler_1 = require("./middleware/errorHandler");
const PORT = process.env.PORT || 3001;
const STORAGE_DIR = path_1.default.resolve(__dirname, "../../../storage");
const CLIENT_DIST = path_1.default.resolve(__dirname, "../../client/dist");
async function main() {
    // 初始化数据库
    const db = (0, database_1.initDatabase)(STORAGE_DIR);
    (0, seed_1.seedTemplates)(db);
    const app = (0, express_1.default)();
    // 中间件
    app.use((0, cors_1.default)());
    app.use(express_1.default.json());
    // API 路由
    app.use("/api/v1/templates", (0, templates_1.templatesRouter)(db));
    app.use("/api/v1/creatives", (0, creatives_1.creativesRouter)(db, STORAGE_DIR, preview_2.onThemeSaved));
    app.use("/api/v1", (0, assets_1.assetsRouter)(db, STORAGE_DIR));
    app.use("/api/v1", (0, builds_1.buildsRouter)(db, STORAGE_DIR, preview_2.onThemeSaved));
    app.use("/api/v1", (0, preview_1.previewRouter)(db, STORAGE_DIR));
    // 构建预览文件（真机扫码测试）
    const previewsDir = path_1.default.join(STORAGE_DIR, "previews");
    app.use("/q", express_1.default.static(previewsDir));
    app.use("/q", (_req, res) => res.status(404).send("Preview not found"));
    // 生产环境：serve React 静态文件
    app.use(express_1.default.static(CLIENT_DIST));
    app.get("*", (_req, res) => {
        res.sendFile(path_1.default.join(CLIENT_DIST, "index.html"));
    });
    // 错误处理
    app.use(errorHandler_1.errorHandler);
    app.listen(PORT, () => {
        console.log(`[platform] Server running at http://localhost:${PORT}`);
        console.log(`[platform] Storage: ${STORAGE_DIR}`);
    });
}
main().catch((err) => {
    console.error("Failed to start server:", err);
    process.exit(1);
});
//# sourceMappingURL=index.js.map