package top.continew.admin.controller.business;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import top.continew.admin.business.model.query.HomeworkQuery;
import top.continew.admin.business.model.req.HomeworkAddReq;
import top.continew.admin.business.model.req.HomeworkEditReq;
import top.continew.admin.business.model.resp.HomeworkResp;
import top.continew.admin.business.service.HomeworkService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * 作业管理 API
 *
 * @author jiay
 * @since 2025/06/26 10:24
 */
@Tag(name = "作业 API")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/homework")
public class HomeworkController {
    private final HomeworkService homeworkService;

    @Operation(summary = "添加作业", description = "添加作业")
    @PostMapping("/add")
    public R<Void> add(@RequestBody @Valid HomeworkAddReq req) {
        return R.ok(homeworkService.addHomework(req));
    }

    @Operation(summary = "编辑作业", description = "编辑作业")
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody @Valid HomeworkEditReq req) {
        return homeworkService.editHomework(req) ? R.ok() : R.fail();
    }

    @Operation(summary = "删除作业", description = "删除作业")
    @DeleteMapping("/delete")
    public R<Void> delete(@RequestParam Long id) {
        return homeworkService.deleteHomework(id) ? R.ok() : R.fail();
    }

    @Operation(summary = "获取作业", description = "获取作业")
    @GetMapping("/get")
    public R<HomeworkResp> getHomework(@RequestParam(name = "id", required = true) Long id) {
        return R.ok(homeworkService.getHomework(id));
    }

    @Operation(summary = "查询作业列表", description = "查询作业列表")
    @GetMapping("/list")
    public R<List<HomeworkResp>> getHomeworkList(HomeworkQuery query) {
        return R.ok(homeworkService.getHomeworkList(query));
    }

    @Operation(summary = "分页查询作业列表", description = "分页查询作业列表")
    @GetMapping
    public R<PageResp<HomeworkResp>> page(HomeworkQuery query, @Validated PageQuery pageQuery) {
        return R.ok(homeworkService.getHomeworkPage(query, pageQuery));
    }

}