package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.admin.request.AdminAiTaskPageReq;
import com.seer.teach.admin.request.AdminAiTaskReq;
import com.seer.teach.admin.response.AdminAiTaskResp;
import com.seer.teach.admin.service.AdminAiTaskService;
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 lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * AI总任务控制器
 *
 * @author System
 * @since 2025-12-11
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/teacher/ai/task")
@Tag(name = "管理端 - AI总任务管理")
public class AdminAiTaskController {

    private final AdminAiTaskService adminAiTaskService;

    @PostMapping("/paged")
    @Operation(summary = "分页查询AI任务")
    @SaCheckPermission("teacher:admin:ai-task:list")
    public ResultBean<PageListBean<AdminAiTaskResp>> pageList(@RequestBody AdminAiTaskPageReq pageRequest) {
        return ResultBean.success(adminAiTaskService.pageList(pageRequest));
    }

    @PostMapping("/list")
    @Operation(summary = "获取AI任务列表")
    @SaCheckPermission("teacher:admin:ai-task:list")
    public ResultBean<List<AdminAiTaskResp>> list(@RequestBody AdminAiTaskPageReq queryRequest) {
        return ResultBean.success(adminAiTaskService.list(queryRequest));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取AI任务")
    @SaCheckPermission("teacher:admin:ai-task:detail")
    public ResultBean<AdminAiTaskResp> getById(@PathVariable Long id) {
        return ResultBean.success(adminAiTaskService.getDetail(id));
    }

    @PostMapping("/start")
    @Operation(summary = "开始AI任务")
    @SaCheckPermission("teacher:admin:ai-task:start")
    public ResultBean<Boolean> start(@RequestBody AdminAiTaskReq requestParam) {
        return ResultBean.success(adminAiTaskService.start(requestParam));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新AI任务")
    @SaCheckPermission("teacher:admin:ai-task:update")
    public ResultBean<Boolean> update(@PathVariable("id") Long id, @RequestBody AdminAiTaskReq requestParam) {
        requestParam.setId(id);
        return ResultBean.success(adminAiTaskService.updateById(requestParam));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除AI任务")
    @SaCheckPermission("teacher:admin:ai-task:delete")
    public ResultBean<Boolean> delete(@PathVariable Long id) {
        return ResultBean.success(adminAiTaskService.deleteById(id));
    }
    
    @PostMapping("/{id}/stop")
    @Operation(summary = "终止AI任务")
    @SaCheckPermission("teacher:admin:ai-task:stop")
    public ResultBean<Boolean> stop(@PathVariable Long id) {
        return ResultBean.success(adminAiTaskService.stopTask(id));
    }
}