package cn.novalon.manage.app.config;

import cn.novalon.manage.sys.handler.auth.SysAuthHandler;
import cn.novalon.manage.sys.handler.config.SysConfigHandler;
import cn.novalon.manage.sys.handler.dictionary.DictionaryHandler;
import cn.novalon.manage.sys.handler.dict.SysDictHandler;
import cn.novalon.manage.sys.handler.log.SysLogHandler;
import cn.novalon.manage.sys.handler.log.OperationLogHandler;
import cn.novalon.manage.sys.handler.menu.MenuHandler;
import cn.novalon.manage.sys.handler.role.SysRoleHandler;
import cn.novalon.manage.sys.handler.permission.SysPermissionHandler;
import cn.novalon.manage.sys.handler.stats.StatsHandler;
import cn.novalon.manage.sys.handler.user.SysUserHandler;
import cn.novalon.manage.notify.handler.SysNoticeHandler;
import cn.novalon.manage.notify.handler.SysUserMessageHandler;
import cn.novalon.manage.file.handler.SysFileHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * 系统路由配置类
 * 
 * 文件定义：配置WebFlux函数式路由，将HTTP请求映射到对应的Handler方法
 * 涉及业务：用户、角色、字典、菜单、公告、文件等所有RESTful API路由
 * 算法：使用RouterFunctions.route()构建函数式路由规则
 * 
 * @author 张翔
 * @date 2026-03-13
 */
@Configuration
public class SystemRouter {

    @Bean
    public RouterFunction<ServerResponse> systemRoutes(
            DictionaryHandler dictionaryHandler,
            SysUserHandler userHandler,
            MenuHandler menuHandler,
            SysRoleHandler roleHandler,
            SysConfigHandler configHandler,
            SysLogHandler logHandler,
            OperationLogHandler operationLogHandler,
            SysAuthHandler authHandler,
            StatsHandler statsHandler,
            SysDictHandler dictHandler,
            SysNoticeHandler noticeHandler,
            SysUserMessageHandler messageHandler,
            SysFileHandler fileHandler,
            SysPermissionHandler permissionHandler) {
        
        return route()
                // ========== 字典路由 ==========
                .GET("/api/dictionaries", dictionaryHandler::getAllDictionaries)
                .GET("/api/dictionaries/{id}", dictionaryHandler::getDictionaryById)
                .GET("/api/dictionaries/type/{type}", dictionaryHandler::getDictionariesByType)
                .GET("/api/dictionaries/check/exists", dictionaryHandler::checkTypeAndCodeExists)
                .POST("/api/dictionaries", dictionaryHandler::createDictionary)
                .PUT("/api/dictionaries/{id}", dictionaryHandler::updateDictionary)
                .DELETE("/api/dictionaries/{id}", dictionaryHandler::deleteDictionary)
                
                // ========== 用户路由 ==========
                .GET("/api/users", userHandler::getAllUsers)
                .GET("/api/users/page", userHandler::getUsersByPage)
                .GET("/api/users/count", userHandler::getUserCount)
                .GET("/api/users/username/{username}", userHandler::getUserByUsername)
                .GET("/api/users/check/username", userHandler::checkUsernameExists)
                .GET("/api/users/check/email", userHandler::checkEmailExists)
                .POST("/api/users", userHandler::createUser)
                .GET("/api/users/{id}", userHandler::getUserById)
                .PUT("/api/users/{id}", userHandler::updateUser)
                .DELETE("/api/users/{id}", userHandler::deleteUser)
                .POST("/api/users/{id}/action/change-password", userHandler::changePassword)
                .POST("/api/users/{id}/action/logical-delete", userHandler::logicalDeleteUser)
                .POST("/api/users/logical-delete", userHandler::logicalDeleteUsers)
                .POST("/api/users/action/restore", userHandler::restoreUsers)
                .POST("/api/users/{id}/action/restore", userHandler::restoreUser)
                .GET("/api/users/{id}/roles", userHandler::getUserRoles)
                .POST("/api/users/{id}/roles", userHandler::assignRoles)
                
                // ========== 菜单路由 ==========
                .GET("/api/menus", menuHandler::getAllMenus)
                .GET("/api/menus/tree", menuHandler::getMenuTree)
                .GET("/api/menus/{id}", menuHandler::getMenuById)
                .POST("/api/menus", menuHandler::createMenu)
                .PUT("/api/menus/{id}", menuHandler::updateMenu)
                .DELETE("/api/menus/{id}", menuHandler::deleteMenu)
                
                // ========== 角色路由 ==========
                .GET("/api/roles", roleHandler::getAllRoles)
                .GET("/api/roles/page", roleHandler::getRolesByPage)
                .GET("/api/roles/count", roleHandler::getRoleCount)
                .GET("/api/roles/name/{roleName}", roleHandler::getRoleByName)
                .GET("/api/roles/check-name", roleHandler::checkNameExists)
                .GET("/api/roles/{id}", roleHandler::getRoleById)
                .POST("/api/roles", roleHandler::createRole)
                .PUT("/api/roles/{id}", roleHandler::updateRole)
                .DELETE("/api/roles/{id}", roleHandler::deleteRole)
                .POST("/api/roles/{id}/restore", roleHandler::restoreRole)
                .GET("/api/roles/{id}/permissions", permissionHandler::getPermissionsByRoleId)
                .POST("/api/roles/{id}/permissions", permissionHandler::assignPermissionsToRole)
                
                // ========== 配置路由 ==========
                .GET("/api/config", configHandler::getAllConfigs)
                .GET("/api/config/{id}", configHandler::getConfigById)
                .GET("/api/config/key/{configKey}", configHandler::getConfigByKey)
                .POST("/api/config", configHandler::createConfig)
                .PUT("/api/config/{id}", configHandler::updateConfig)
                .DELETE("/api/config/{id}", configHandler::deleteConfig)
                
                // ========== 日志路由 ==========
                .GET("/api/logs/login", logHandler::getAllLoginLogs)
                .GET("/api/logs/login/page", logHandler::getLoginLogsByPage)
                .GET("/api/logs/login/count", logHandler::getLoginLogCount)
                .GET("/api/logs/login/today/count", logHandler::getTodayLoginCount)
                .GET("/api/logs/login/recent", logHandler::getRecentLoginLogs)
                .GET("/api/logs/login/{id}", logHandler::getLoginLogById)
                .POST("/api/logs/login", logHandler::createLoginLog)
                .GET("/api/logs/exception", logHandler::getAllExceptionLogs)
                .GET("/api/logs/exception/page", logHandler::getExceptionLogsByPage)
                .GET("/api/logs/exception/count", logHandler::getExceptionLogCount)
                .GET("/api/logs/exception/{id}", logHandler::getExceptionLogById)
                .POST("/api/logs/exception", logHandler::createExceptionLog)
                .GET("/api/logs/operation", operationLogHandler::getAllOperationLogs)
                .GET("/api/logs/operation/page", operationLogHandler::getOperationLogsByPage)
                .GET("/api/logs/operation/count", operationLogHandler::getOperationLogCount)
                .GET("/api/logs/operation/{id}", operationLogHandler::getOperationLogById)
                .POST("/api/logs/operation", operationLogHandler::createOperationLog)
                
                // ========== 认证路由 ==========
                .POST("/api/auth/login", authHandler::login)
                .POST("/api/auth/register", authHandler::register)
                .POST("/api/auth/logout", authHandler::logout)
                
                // ========== 统计路由 ==========
                .GET("/api/stats/overview", statsHandler::getOverview)
                
                // ========== 数据字典路由 ==========
                .GET("/api/dict/types", dictHandler::getAllDictTypes)
                .GET("/api/dict/types/{id}", dictHandler::getDictTypeById)
                .GET("/api/dict/types/type/{dictType}", dictHandler::getDictTypeByType)
                .POST("/api/dict/types", dictHandler::createDictType)
                .PUT("/api/dict/types/{id}", dictHandler::updateDictType)
                .DELETE("/api/dict/types/{id}", dictHandler::deleteDictType)
                .GET("/api/dict/data", dictHandler::getAllDictData)
                .GET("/api/dict/data/type/{dictType}", dictHandler::getDictDataByType)
                .GET("/api/dict/data/{id}", dictHandler::getDictDataById)
                .POST("/api/dict/data", dictHandler::createDictData)
                .PUT("/api/dict/data/{id}", dictHandler::updateDictData)
                .DELETE("/api/dict/data/{id}", dictHandler::deleteDictData)
                
                // ========== 公告路由 ==========
                .GET("/api/notices", noticeHandler::getAllNotices)
                .GET("/api/notices/{id}", noticeHandler::getNoticeById)
                .GET("/api/notices/status/{status}", noticeHandler::getNoticesByStatus)
                .POST("/api/notices", noticeHandler::createNotice)
                .PUT("/api/notices/{id}", noticeHandler::updateNotice)
                .DELETE("/api/notices/{id}", noticeHandler::deleteNotice)
                
                // ========== 消息路由 ==========
                .GET("/api/messages/user/{userId}", messageHandler::getMessagesByUser)
                .GET("/api/messages/user/{userId}/unread", messageHandler::getUnreadCount)
                .GET("/api/messages/user/{userId}/unread/list", messageHandler::getUnreadList)
                .POST("/api/messages", messageHandler::createMessage)
                .PUT("/api/messages/{id}/read", messageHandler::markAsRead)
                .DELETE("/api/messages/{id}", messageHandler::deleteMessage)
                
                // ========== 文件路由 ==========
                .GET("/api/files", fileHandler::getAllFiles)
                .GET("/api/files/{id}", fileHandler::getFileById)
                .POST("/api/files/upload", fileHandler::uploadFile)
                .GET("/api/files/{id}/download", fileHandler::downloadFile)
                .GET("/api/files/download/{fileName}", fileHandler::downloadFileByName)
                .GET("/api/files/{id}/preview", fileHandler::previewFile)
                .GET("/api/files/preview/{fileName}", fileHandler::previewFileByName)
                .DELETE("/api/files/{id}", fileHandler::deleteFile)
                
                // ========== 权限路由 ==========
                .GET("/api/permissions", permissionHandler::getAllPermissions)
                .GET("/api/permissions/{id}", permissionHandler::getPermissionById)
                .GET("/api/permissions/code/{code}", permissionHandler::getPermissionByCode)
                .GET("/api/permissions/check-code", permissionHandler::checkCodeExists)
                .GET("/api/permissions/count", permissionHandler::getPermissionCount)
                .POST("/api/permissions", permissionHandler::createPermission)
                .PUT("/api/permissions/{id}", permissionHandler::updatePermission)
                .DELETE("/api/permissions/{id}", permissionHandler::deletePermission)
                
                .build();
    }
}
