import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import fs from "fs";
import path from "path";

function mockPlugin() {
  return {
    name: "simple-mock-plugin",
    configureServer(server) {
      server.middlewares.use((req, res, next) => {
        const sendJSON = (file) => {
          const filePath = path.resolve(process.cwd(), `mock/${file}`);
          if (!fs.existsSync(filePath)) {
            res.statusCode = 404;
            res.setHeader("Content-Type", "application/json");
            return res.end(
              JSON.stringify({
                error: `Mock file not found: ${file}`,
              }),
            );
          }
          const json = fs.readFileSync(filePath, "utf-8");
          res.setHeader("Content-Type", "application/json");
          res.end(json);
        };

        // 用户信息
        if (req.url === "/api/userInfo" && req.method === "GET") {
          return sendJSON("userInfo.json");
        }

        // 提交用户信息
        if (req.url === "/api/userInfo/submit" && req.method === "POST") {
          return sendJSON("success.json");
        }

        // 活动列表
        if (req.url.startsWith("/api/actList") && req.method === "GET") {
          return sendJSON("actList.json");
        }

        // 积分概览
        if (
          req.url.startsWith("/api/report/myOverview") &&
          req.method === "GET"
        ) {
          return sendJSON("scoreOverview.json");
        }

        // 排名列表
        if (
          req.url.startsWith("/api/report/rankList") &&
          req.method === "GET"
        ) {
          return sendJSON("rank.json");
        }

        // 活动详情
        if (
          req.url.startsWith("/api/actDetails/details") &&
          req.method === "GET"
        ) {
          return sendJSON("actDetails.json");
        }

        // 申请/撤销活动
        if (
          req.url.startsWith("/api/actDetails/apply") &&
          req.method === "POST"
        ) {
          return sendJSON("success.json");
        }

        // 我的报名列表
        if (req.url.startsWith("/api/myApplyList") && req.method === "GET") {
          return sendJSON("myApplyList.json");
        }

        // 活动来源列表
        if (
          req.url.startsWith("/api/act/publisherList") &&
          req.method === "GET"
        ) {
          return sendJSON("publishers.json");
        }

        // 服务时长列表
        if (
          req.url.startsWith("/api/act/durationsList") &&
          req.method === "GET"
        ) {
          return sendJSON("durationList.json");
        }

        // 上传服务记录
        if (
          req.url.startsWith("/api/act/uploadService") &&
          req.method === "POST"
        ) {
          return sendJSON("success.json");
        }

        // 用户积分
        if (
          req.url.startsWith("/api/service/userScore") &&
          req.method === "GET"
        ) {
          return sendJSON("userScore.json");
        }

        // 年份列表
        if (
          req.url.startsWith("/api/service/yearList") &&
          req.method === "GET"
        ) {
          return sendJSON("yearList.json");
        }

        // 年度积分
        if (
          req.url.startsWith("/api/service/yearScore") &&
          req.method === "GET"
        ) {
          return sendJSON("yearScore.json");
        }

        // 服务记录列表
        if (req.url.startsWith("/api/service/list") && req.method === "GET") {
          return sendJSON("serviceList.json");
        }

        // 服务详情
        if (
          req.url.startsWith("/api/service/details") &&
          req.method === "GET"
        ) {
          return sendJSON("recordDetails.json");
        }

        next();
      });
    },
  };
}

export default defineConfig({
  plugins: [vue(), mockPlugin()],
});
