package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seer.teach.admin.request.GenerateTaskReq;
import com.seer.teach.admin.request.TeachGoalCheckAndScoreReq;
import com.seer.teach.admin.request.TeachGoalsBatchSaveReq;
import com.seer.teach.admin.request.TeachGoalsQueryReq;
import com.seer.teach.admin.request.TeachGoalsReq;
import com.seer.teach.admin.response.TeachGoalsResp;
import com.seer.teach.admin.service.AdminTeachGoalsService;
import com.seer.teach.admin.request.GenerateTeachGoalReq;
import com.seer.teach.admin.service.TeachGoalGenerationService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 管理端 - 教学目标管理
 * </p>
 *
 * @author Lingma
 * @since 2025-12-04
 */
@RestController
@RequestMapping("/teacher/teach-goals")
@RequiredArgsConstructor
@Tag(name = "管理端 - 教学目标管理")
@Validated
public class AdminTeachGoalsController {

    private final AdminTeachGoalsService adminTeachGoalsService;
    private final TeachGoalGenerationService teachGoalGenerationService;

    @PostMapping("/page-list")
    @Operation(summary = "分页查询教学目标列表")
    @SaCheckPermission("teacher:admin:teach-goals:list")
    public ResultBean<PageListBean<TeachGoalsResp>> pageTeachGoals(@RequestBody @Validated TeachGoalsQueryReq req) {
        IPage<TeachGoalsResp> page = adminTeachGoalsService.pageTeachGoals(req);
        return ResultBean.success(new PageListBean<>(page));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取教学目标详情")
    @SaCheckPermission("teacher:admin:teach-goals:detail")
    public ResultBean<TeachGoalsResp> getTeachGoalById(@Parameter(description = "教学目标ID") @PathVariable @NotNull(message = "教学目标ID不能为空") Integer id) {
        TeachGoalsResp teachGoal = adminTeachGoalsService.getTeachGoalById(id);
        return ResultBean.success(teachGoal);
    }

    @PostMapping
    @Operation(summary = "添加教学目标")
    @SaCheckPermission("teacher:admin:teach-goals:save")
    public ResultBean<Boolean> addTeachGoal(@Parameter(description = "教学目标信息") @Valid @RequestBody TeachGoalsReq req) {
        boolean result = adminTeachGoalsService.addTeachGoal(req);
        return ResultBean.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新教学目标信息")
    @SaCheckPermission("teacher:admin:teach-goals:update")
    public ResultBean<Boolean> updateTeachGoal(@Parameter(description = "教学目标ID") @PathVariable @NotNull(message = "教学目标ID不能为空") Integer id,
                                               @Parameter(description = "教学目标信息") @Valid @RequestBody TeachGoalsReq req) {
        boolean result = adminTeachGoalsService.updateTeachGoal(id, req);
        return ResultBean.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除教学目标")
    @SaCheckPermission("teacher:admin:teach-goals:delete")
    public ResultBean<Boolean> deleteTeachGoal(@Parameter(description = "教学目标ID") @PathVariable @NotNull(message = "教学目标ID不能为空") Integer id) {
        boolean result = adminTeachGoalsService.deleteTeachGoal(id);
        return ResultBean.success(result);
    }

    @PutMapping("/{id}/check-and-score")
    @Operation(summary = "更新教学目标检查状态和评分")
    @SaCheckPermission("teacher:admin:teach-goals:update")
    public ResultBean<Boolean> updateTeachGoalCheckAndScore(@Parameter(description = "教学目标ID") @PathVariable @NotNull(message = "教学目标ID不能为空") Integer id,
                                                           @Parameter(description = "检查状态和评分信息") @Valid @RequestBody TeachGoalCheckAndScoreReq req) {
        boolean result = adminTeachGoalsService.updateTeachGoalCheckAndScore(id, req);
        return ResultBean.success(result);
    }

    @PutMapping("/{id}/sort/{sort}")
    @Operation(summary = "更新教学目标的排序")
    @SaCheckPermission("teacher:admin:teach-goals:sort:update")
    public ResultBean<Boolean> updateTeachGoalSort(@Parameter(description = "教学目标ID") @PathVariable @NotNull(message = "教学目标ID不能为空") Integer id,
                                                   @PathVariable @NotNull(message = "教学目标的排序") Integer sort) {
        boolean result = adminTeachGoalsService.updateTeachGoalSort(id, sort);
        return ResultBean.success(result);
    }

    @PostMapping("/batch-save-generated")
    @Operation(summary = "批量保存生成的教学目标")
    @SaCheckPermission("teacher:admin:teach-goals:batch-save")
    public ResultBean<Boolean> batchSaveGeneratedTeachGoals(@Parameter(description = "教学目标信息列表") @Valid @RequestBody TeachGoalsBatchSaveReq req) {
        boolean result = adminTeachGoalsService.batchSaveGeneratedTeachGoals(req);
        return ResultBean.success(result);
    }

    @PostMapping("/generate-similar")
    @Operation(summary = "根据知识点生成教学目标")
    @SaCheckPermission("teacher:admin:teach-goals:generate")
    public ResultBean<List<TeachGoalsResp>> generateSimilarTeachGoals(@Parameter(description = "生成教学目标参数") @Valid @RequestBody GenerateTaskReq req) {
        req.setUserId(StpUtil.getLoginIdAsInt());
        List<TeachGoalsResp> result = teachGoalGenerationService.generateTeachGoals(req);
        return ResultBean.success(result);
    }

    @PostMapping("/generate-all-and-save")
    @Operation(summary = "生成所有教学目标并保存")
    @SaCheckPermission("teacher:admin:teach-goals:generate")
    public ResultBean<Integer> generateAllTeachGoalsAndSave(@Parameter(description = "生成教学目标参数") @Valid @RequestBody GenerateTaskReq req) {
        req.setUserId(StpUtil.getLoginIdAsInt());
        Integer result = teachGoalGenerationService.generateAndSaveAllTeachGoals(req);
        return ResultBean.success(result);
    }
}