package com.seer.teach.teacher.controller.app;

import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.teacher.common.req.AppBookReadReq;
import com.seer.teach.teacher.common.req.BookPageReq;
import com.seer.teach.teacher.common.resp.AppBookInfoResp;
import com.seer.teach.teacher.common.resp.AppChapterInfoResp;
import com.seer.teach.teacher.common.resp.AudioData;
import com.seer.teach.teacher.common.resp.BookHistoryResp;
import com.seer.teach.teacher.module.entity.IdEntity;
import com.seer.teach.teacher.service.ReadBookService;
import com.seer.teach.teacher.service.registry.smartRead.SmartReadHandler;
import com.seer.teach.teacher.service.registry.smartRead.SmartReadRegistry;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 智能阅读控制器，提供与智能听书、读书历史相关的接口。
 */
@RequestMapping("/app/smartRead")
@RestController
@Tag(name = "APP - 智读")
@EncryptionAnnotation
@DecryptionAnnotation
@Validated
@RequiredArgsConstructor
public class SmartReadController {

    private final SmartReadRegistry smartReadRegistry;

    private final ReadBookService readBookService;

    @PostMapping("/list")
    @LogPrint
    @Operation(summary = "list")
    public ResultBean<PageListBean<AppBookInfoResp>> recentReading(
            @RequestBody @Validated BookPageReq param) {
        // 获取当前登录用户ID
        Integer userId = StpUtil.getLoginIdAsInt();
        // 根据类型获取对应的处理器
        SmartReadHandler handler = smartReadRegistry.getHandler(param.getType());
        // 调用处理器获取书籍列表数据
        PageListBean<AppBookInfoResp> bookList = handler.getBookList(userId, param);
        return ResultBean.success(bookList);
    }

    @PostMapping("/history")
    @LogPrint
    @Operation(summary = "历史记录")
    public ResultBean<BookHistoryResp> history() {
        // 调用服务层方法获取用户的历史记录
        BookHistoryResp result = readBookService.getUserHistory();
        return ResultBean.success(result);
    }

    @PostMapping("/count")
    @LogPrint
    @Operation(summary = "用户数本数量")
    public ResultBean<HashMap<String, Object>> count() {
        Integer count = readBookService.getUserBookCount();
        HashMap<String, Object> results = new HashMap<>();
        results.put("count", count);
        return ResultBean.success(results);
    }

    @PostMapping("/chapters")
    @LogPrint
    @Operation(summary = "章节列表")
    public ResultBean<AppChapterInfoResp> chapters(@RequestBody @Validated IdEntity params) {
        AppChapterInfoResp results = readBookService.chapters(params.getId());
        return ResultBean.success(results);
    }

    @PostMapping("/read")
    @LogPrint
    @Operation(summary = "阅读")
    public ResultBean<List<AudioData>> read(@RequestBody @Validated AppBookReadReq params) {
       List<AudioData> results =  readBookService.read(params);
    	return ResultBean.success(results);
    }

}
