package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.admin.request.BooksQueryReq;
import com.seer.teach.admin.request.BooksSaveReq;
import com.seer.teach.admin.request.BooksUpdateReq;
import com.seer.teach.admin.response.BooksListResp;
import com.seer.teach.admin.response.BooksResp;
import com.seer.teach.admin.service.BooksAdminService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Tag(name = "后台-书籍管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/book")
public class BooksAdminController {

    private final BooksAdminService booksAdminService;


    @PostMapping("/page-list")
    @Operation(summary = "书籍分页查询")
    @SaCheckPermission("teacher:admin:books:list")
    public ResultBean<PageListBean<BooksListResp>> pageList(@RequestBody @Validated BooksQueryReq params){
        PageListBean<BooksListResp> pageList = booksAdminService.pageList(params);
        return ResultBean.success(pageList);
    }

    @PostMapping("/save")
    @Operation(summary = "新增书籍")
    @SaCheckPermission("teacher:admin:books:save")
    public ResultBean saveBooks(@RequestBody @Validated BooksSaveReq params){
        booksAdminService.saveBooks(params);
        return ResultBean.success();
    }

    @PostMapping("/en-sale")
    @Operation(summary = "上架书籍")
    @SaCheckPermission("teacher:admin:books:en-sale")
    public ResultBean enSale(@RequestBody List<Integer> ids){
        booksAdminService.enSale(ids);
        return ResultBean.success();
    }

    @PostMapping("/un-sale")
    @Operation(summary = "下架书籍")
    @SaCheckPermission("teacher:admin:books:un-sale")
    public ResultBean unSale(@RequestBody List<Integer> ids){
        booksAdminService.unSale(ids);
        return ResultBean.success();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除书籍")
    @SaCheckPermission("teacher:admin:books:delete")
    public ResultBean deleteBooks(@RequestBody List<Integer> ids) {
        booksAdminService.deleteBooks(ids);
        return ResultBean.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "查看书籍详情")
    @SaCheckPermission("teacher:admin:books:detail")
    public ResultBean<BooksResp> detail(@PathVariable("id") @NotNull(message = "书籍ID不可为空") Integer id) {
        BooksResp result = booksAdminService.detail(id);
        return ResultBean.success(result);
    }

    @PostMapping("/update")
    @Operation(summary = "修改书籍")
    @SaCheckPermission("teacher:admin:books:update")
    public ResultBean update(@RequestBody @Validated BooksUpdateReq req){
        booksAdminService.updateBooksById(req);
        return ResultBean.success();
    }

}
