package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.demo.domain.dto.KaoshixingExamListRequest;
import org.dromara.demo.service.impl.KaoshixingService;
import org.dromara.main.domain.bo.MainExamEvaluationBo;
import org.dromara.main.domain.vo.MainExamEvaluationVo;
import org.dromara.main.service.IMainExamEvaluationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/main/examEvaluation")
public class MainExamEvaluationController extends BaseController {

    private final IMainExamEvaluationService examEvaluationService;
    private final KaoshixingService kaoshixingService;

    /**
     * 查询测评列表
     */
    @SaCheckPermission("main:examEvaluation:list")
    @GetMapping("/list")
    public TableDataInfo<MainExamEvaluationVo> list(MainExamEvaluationBo bo, PageQuery pageQuery) {
        return examEvaluationService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取测评详细信息
     */
    @SaCheckPermission("main:examEvaluation:query")
    @GetMapping("/{id}")
    public R<MainExamEvaluationVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(examEvaluationService.queryById(id));
    }

    /**
     * 新增测评
     */
    @SaCheckPermission("main:examEvaluation:add")
    @Log(title = "测评管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MainExamEvaluationBo bo) {
        return toAjax(examEvaluationService.insertByBo(bo));
    }

    /**
     * 修改测评
     */
    @SaCheckPermission("main:examEvaluation:edit")
    @Log(title = "测评管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MainExamEvaluationBo bo) {
        return toAjax(examEvaluationService.updateByBo(bo));
    }

    /**
     * 删除测评
     */
    @SaCheckPermission("main:examEvaluation:remove")
    @Log(title = "测评管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(examEvaluationService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 批量更新状态
     */
    @SaCheckPermission("main:examEvaluation:edit")
    @Log(title = "测评管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public R<Void> updateStatus(@RequestBody List<Long> ids, @RequestParam String status) {
        return toAjax(examEvaluationService.updateStatus(ids, status));
    }

    /**
     * 同步第三方数据
     */
    @SaCheckPermission("main:examEvaluation:sync")
    @Log(title = "测评管理", businessType = BusinessType.OTHER)
    @PostMapping("/sync")
    public R<Void> syncThirdPartyData() {
        return toAjax(examEvaluationService.syncThirdPartyData());
    }



    @SaCheckPermission("main:evaluation:list")
    @ResponseBody
    @PostMapping("/exam-list")
    @Log(title = "获取第三方考试列表", businessType = BusinessType.OTHER)
    public R<String> getExamList(@RequestBody Map<String, Object> params) {
        // 写死的第三方凭证
        String appId = "631088";  // TODO: 后续改为配置
        String appKey = "605f5f51895f423195850957bb903a43"; // TODO: 后续改为配置

        // 构建请求参数
        KaoshixingExamListRequest request = new KaoshixingExamListRequest();
        request.setAppId(appId);
        request.setAppKey(appKey);
        request.setPage((Integer) params.getOrDefault("page", 1));

        // 转换日期格式：2026-01-01 -> 2026-01-01 00:00:00
        String examStartTime = (String) params.get("examStartTime");
        if (examStartTime != null && !examStartTime.isEmpty()) {
            request.setExamStartTime(examStartTime + " 00:00:00");
        }

        String examEndTime = (String) params.get("examEndTime");
        if (examEndTime != null && !examEndTime.isEmpty()) {
            request.setExamEndTime(examEndTime + " 23:59:59");
        }

        request.setExamIds((String) params.get("examIds"));
        request.setExpSeconds((Integer) params.getOrDefault("expSeconds", 300));

        // 调用第三方接口
        String result = kaoshixingService.fetchExamList(request);
        return R.ok(result);
    }
}
