package com.sundot.airport.web.controller.exam;

import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.page.TableDataInfo;
import com.sundot.airport.common.dto.WeakModuleDetailDTO;
import com.sundot.airport.exam.domain.WeakModuleRule;
import com.sundot.airport.exam.dto.WeakModuleRuleDTO;
import com.sundot.airport.exam.service.IWeakModuleRuleService;
import com.sundot.airport.exam.service.IWeakModuleStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b>功能名：</b>WeakModuleRuleController<br>
 * <b>说明：</b> 薄弱项出题规则管理Controller <br>
 * <b>著作权：</b> Copyright (C) 2025 SUNDOT CORPORATION<br>
 * <b>修改履历：
 *
 * @author Simon Lin
 */
@Api(tags = "薄弱项出题规则管理")
@RestController
@RequestMapping("/exam/weakModuleRule")
public class WeakModuleRuleController extends BaseController {

    @Autowired
    private IWeakModuleRuleService weakModuleRuleService;

    @Autowired
    private IWeakModuleStatisticsService weakModuleStatisticsService;

    /**
     * 查询规则列表
     */
    @ApiOperation("查询规则列表")
    @PreAuthorize("@ss.hasPermi('exam:weakModuleRule:list') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<WeakModuleRule> list = weakModuleRuleService.list();
        return getDataTable(list);
    }

    /**
     * 获取规则详情
     */
    @ApiOperation("获取规则详情")
    @PreAuthorize("@ss.hasPermi('exam:weakModuleRule:query') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(weakModuleRuleService.getById(id));
    }

    /**
     * 新增规则
     */
    @ApiOperation("新增规则")
    @PreAuthorize("@ss.hasPermi('exam:weakModuleRule:add') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WeakModuleRuleDTO dto) {
        return toAjax(weakModuleRuleService.insert(dto));
    }

    /**
     * 修改规则
     */
    @ApiOperation("修改规则")
    @PreAuthorize("@ss.hasPermi('exam:weakModuleRule:edit') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WeakModuleRuleDTO dto) {
        return toAjax(weakModuleRuleService.update(dto));
    }

    /**
     * 删除规则
     */
    @ApiOperation("删除规则")
    @PreAuthorize("@ss.hasPermi('exam:weakModuleRule:remove') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(weakModuleRuleService.deleteById(id));
    }

    /**
     * 启用规则
     */
    @ApiOperation("启用规则（同时只能有一个启用）")
    @PreAuthorize("@ss.hasPermi('exam:weakModuleRule:edit') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @PutMapping("/enable/{id}")
    public AjaxResult enable(@PathVariable Long id) {
        return toAjax(weakModuleRuleService.enable(id));
    }

    /**
     * 获取当前启用的规则
     */
    @ApiOperation("获取当前启用的规则")
    @GetMapping("/active")
    public AjaxResult getActive() {
        WeakModuleRule activeRule = weakModuleRuleService.getActiveRule();
        if (activeRule == null) {
            return warn("当前没有启用的规则");
        }
        return success(activeRule);
    }

    /**
     * 获取用户的TOP3薄弱模块详细信息（用于验证算法）
     */
    @ApiOperation("获取用户的TOP3薄弱模块详细信息")
    @GetMapping("/weakModules/{userId}")
    public AjaxResult getWeakModulesDetail(
            @ApiParam(value = "用户ID", required = true) @PathVariable Long userId) {
        List<WeakModuleDetailDTO> details = weakModuleStatisticsService.getTop3WeakModulesDetail(userId);
        return success(details);
    }

    /**
     * 获取用户的TOP3薄弱模块详细信息及出题计划（用于验证出题算法）
     */
    @ApiOperation("获取用户的TOP3薄弱模块详细信息及出题计划")
    @GetMapping("/weakModules/{userId}/allocation")
    public AjaxResult getWeakModulesDetailWithAllocation(
            @ApiParam(value = "用户ID", required = true) @PathVariable Long userId,
            @ApiParam(value = "题目数量", required = true) @RequestParam Integer questionCount) {
        List<WeakModuleDetailDTO> details = weakModuleStatisticsService
                .getTop3WeakModulesDetailWithAllocation(userId, questionCount);
        return success(details);
    }
}
