package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.admin.convert.SubjectConvert;
import com.seer.teach.admin.request.GradeSubjectReq;
import com.seer.teach.admin.response.GradeSubjectResp;
import com.seer.teach.admin.response.SubjectResp;
import com.seer.teach.admin.service.GradeSubjectAdminService;
import com.seer.teach.common.ResultBean;
import com.seer.teach.teacher.module.entity.SubjectsEntity;
import com.seer.teach.teacher.service.SubjectsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Tag(name = "后台-科目管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/grade-subject")
public class GradeSubjectAdminController {

    private final GradeSubjectAdminService gradeSubjectService;

    private final SubjectsService subjectsService;

    /**
     * 获取年级科目列表
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "获取年级科目列表")
    @SaCheckPermission("admin:grade-subject:list")
    public ResultBean<List<GradeSubjectResp>> list(){
        List<GradeSubjectResp> list = gradeSubjectService.listSubjectsGrade();
        return ResultBean.success(list);
    }

    /**
     * 新增科目
     * @param params
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "新增科目")
    @SaCheckPermission("admin:grade-subject:create")
    public ResultBean create(@RequestBody @Validated GradeSubjectReq params){
        gradeSubjectService.addGradeSubject(params);
        return ResultBean.success();
    }

    /**
     * 更新科目状态
     * @param params
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "修改科目状态")
    @SaCheckPermission("admin:grade-subject:update")
    public ResultBean update(@RequestBody @Validated GradeSubjectReq params){
        gradeSubjectService.updateGradeSubject(params);
        return ResultBean.success();
    }

    /**
     * 删除科目
     * @param relationId
     * @return
     */
    @DeleteMapping("/{relationId}")
    @Operation(summary = "删除科目")
    @SaCheckPermission("admin:grade-subject:delete")
    public ResultBean delete(@PathVariable("relationId") @NotNull(message = "关联ID不可为空") Integer relationId){
        gradeSubjectService.deleteGradeSubject(relationId);
        return ResultBean.success();
    }

    /**
     * 获取所有科目列表
     * @return
     */
    @GetMapping("/subjects/list")
    @Operation(summary = "科目列表")
    @SaCheckPermission("admin:grade-subject:subjects:list")
    public ResultBean<List<SubjectResp>> SubjectsList(){
        List<SubjectsEntity> list = subjectsService.getSubjectList();
        List<SubjectResp> results =  SubjectConvert.INSTANCE.convertRespList(list);
        return ResultBean.success(results);
    }
}
