import { Router } from "express";
import userController from "../controllers/userController";
import artController from "../controllers/artController";
import doneRateController from "../controllers/doneRateController";
import messageTemplateController from "../controllers/messageTemplateController";
import messageStrategyController from "../controllers/messageStrategyController"; // 新增：导入消息策略控制器
import messageActivityController from "../controllers/messageActivityController"; // 新增：导入消息活动控制器
import messageRecordController from "../controllers/messageRecordController"; // 新增：导入消息记录控制器
import UserTargetingController from "../controllers/userTargetingController";
import adminController from "../controllers/adminController";
import { authMiddleware } from "../middleware/authMiddleware";

const router = Router();

// 公共的管理员认证路由 (无需认证)
router.post("/admin/register", adminController.register);
router.post("/admin/login", adminController.login);

router.post("/users/send-message", userController.sendDirectMessage); // 点对点

// 新增：消息记录统计路由
router.get("/message/statistics/overall", messageRecordController.getOverallStatistics);
router.get("/message/statistics/by-activity", messageRecordController.getStatisticsByActivity);
router.get("/message/statistics/by-strategy", messageRecordController.getStatisticsByStrategy);
router.get("/message/statistics/by-template", messageRecordController.getStatisticsByTemplate);
router.get("/message/statistics/by-cc", messageRecordController.getStatisticsByCc);
router.get("/message/statistics/by-image", messageRecordController.getStatisticsByImage);
router.get("/message/statistics/daily-trends", messageRecordController.getDailySentTrends);
router.get("/message/statistics/multi-dimensional", messageRecordController.getMultiDimensionalStatistics); // 新增：多维度统计路由

// 新增：下钻统计路由
router.get("/message/daily/trends/by-cc/:cc", messageRecordController.getDailyTrendsByCc);
router.get("/message/daily/trends/by-strategy/:strategyName", messageRecordController.getDailyTrendsByStrategy);
router.get("/message/daily/trends/by-template/:templateName", messageRecordController.getDailyTrendsByTemplate);
router.get("/message/daily/trends/by-image/:image", messageRecordController.getDailyTrendsByImage);

// 应用认证中间件，保护所有下面的路由
router.use(authMiddleware);

// User routes
router.post("/users", userController.createUser);
// Updated to the paginated user list interface
router.get("/users", userController.getPaginatedUsers); // GET /api/users?page=1&limit=10&project=1
router.get("/users/:uid", userController.getUserByUid);
router.put("/users/:uid", userController.updateUser);
router.delete("/users/:uid", userController.deleteUser);
// 新增：发送消息接口

router.post("/users/send-bulk-message", userController.sendBulkMessage); // 群发

router.get("/arts", artController.getArts); // 获取作品列表 (支持分页、筛选、排序)
router.get("/arts/:id", artController.getArtById); // 获取单个作品
router.put("/arts/:id", artController.updateArt); // 更新作品信息

// 完成率 DoneRate 路由 (只读)
router.get("/done-rates/artwork/:resId", doneRateController.getDoneRatesByArtworkId); // 按作品 ID 获取历史完成率
router.get("/done-rates/date/:date", doneRateController.getDoneRatesByDate); // 按日期获取所有作品完成率

// 消息模板路由
router.post("/message-template", messageTemplateController.createTemplate);
router.get("/message-template", messageTemplateController.getAllTemplates);
router.get("/message-template/:templateName", messageTemplateController.getTemplateByName);
router.put("/message-template/:templateName", messageTemplateController.updateTemplate);
router.delete("/message-template/:templateName", messageTemplateController.deleteTemplate);

// 新增：消息策略路由
router.post("/message-strategy", messageStrategyController.createStrategy);
router.get("/message-strategies", messageStrategyController.getStrategies);
router.get("/message-strategy/:id", messageStrategyController.getStrategyById);
router.put("/message-strategy/:id", messageStrategyController.updateStrategy);
router.delete("/message-strategy/:id", messageStrategyController.deleteStrategy);

// 新增：消息活动路由
router.post("/message-activity", messageActivityController.createActivity);
router.get("/message-activities", messageActivityController.getActivities);
router.get("/message-activity/:id", messageActivityController.getActivityById);
router.put("/message-activity/:id", messageActivityController.updateActivity);
router.delete("/message-activity/:id", messageActivityController.deleteActivity);
router.put("/message-activity/:id/status", messageActivityController.updateActivityStatus);

// 新增：用户筛选相关路由
router.post("/users/count", UserTargetingController.countTargetUsers);

// 新增：消息记录路由
router.post("/message-record", messageRecordController.createRecord);
router.get("/message-records", messageRecordController.getPaginatedRecords); // 新增分页接口
router.get("/message-records/user/:uid", messageRecordController.getRecordsByUid);
router.get("/message-record/:id", messageRecordController.getRecordById);
router.put("/message-record/:id", messageRecordController.updateRecord);

// 管理员路由
router.get("/admin", adminController.getAdmins);
router.get("/admin/:id", adminController.getAdminById);
router.put("/admin/:id", adminController.updateAdmin);
router.delete("/admin/:id", adminController.deleteAdmin);

export default router;
