package cn.iocoder.yudao.module.train.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.base.constant.SchoolBaseConstants;
import cn.iocoder.yudao.module.base.service.SchoolNotifyMessageSendService;
import cn.iocoder.yudao.module.train.entity.ReservationCourse;
import cn.iocoder.yudao.module.train.service.ReservationCourseService;
import cn.iocoder.yudao.module.train.vo.ReservationCourseVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.base.utils.DriveSchoolBaseUtil.getSubjectStr;

@Tag(name = "管理后台 - 预约练车")
@RestController
@RequestMapping("/reservation-course")
@Validated
public class ReservationCourseController {

    @Resource
    private ReservationCourseService reservationCourseService;

    @Resource
    private SchoolNotifyMessageSendService schoolNotifyMessageSendService;


    @GetMapping("/page")
    @Operation(summary = "获得预约练车分页")
    public CommonResult<IPage<?>> getPage(ReservationCourseVO pageReqVO,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<ReservationCourseVO> page = new Page<>(pageNo, pageSize);
        if ("my".equals(pageReqVO.getSelectType())) {
            //查自己的
            Long userId = SecurityFrameworkUtils.getLoginUserId();
            pageReqVO.setCoachId(userId);
        }
        //查未取消的
        pageReqVO.setIfCancel(false);
        return success(reservationCourseService.queryListPage(pageReqVO, page));
    }


    @PutMapping("/update")
    @Operation(summary = "审核预约练车")
    public CommonResult<Boolean> updateReservationCourse(@RequestBody ReservationCourseVO updateReqVO) {
        reservationCourseService.updateById(updateReqVO);

        //通过id查询预约记录
        ReservationCourse createObj = reservationCourseService.getById(updateReqVO.getId());

        // 准备内容
        String message = String.format(SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TEMPLATE_COACH_RESERVE_RESULT, createObj.getReservDay(), createObj.getReservTime(), createObj.getCourseName(), getSubjectStr(createObj.getSubject()), reservatStatusStr(updateReqVO.getStatus()));

        // 发送消息
        schoolNotifyMessageSendService.sendMessage(createObj.getUserId(), message, SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TYPE_MEMBER, createObj.getTenantId());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预约练车")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:reservation-course:delete')")
    public CommonResult<Boolean> deleteReservationCourse(@RequestParam("id") String id) {
        reservationCourseService.removeById(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预约练车")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:reservation-course:query')")
    public CommonResult<ReservationCourse> getReservationCourse(@RequestParam("id") String id) {
        return success(reservationCourseService.getById(id));
    }

    /**
     * 根据学生userId查询学生信息及当天的预约记录
     *
     * @param userId 学生userId
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.train.entity.ReservationCourse>
     * @author vinjor-M
     * @date 17:25 2025/1/16
     **/
    @GetMapping("/getUserInfoAndReservation")
    @Operation(summary = "根据学生userId查询学生信息及当天的预约记录")
    @Parameter(name = "id", description = "学生userId", required = true, example = "1024")
    public CommonResult<?> getReservationCourse(@RequestParam("userId") Long userId,
                                                @RequestParam("courseId") String courseId,
                                                @RequestParam("type") String type,
                                                @RequestParam("subject") Integer subject) {
        return success(reservationCourseService.getUserInfoAndReservation(userId, courseId, subject, type));
    }

    public String reservatStatusStr(Boolean status) {
        if (!status) {
            return "拒绝";
        } else{
            return "通过";
        }
    }

}