package top.continew.admin.controller.business;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.business.model.query.SubtitleStylePageQuery;
import top.continew.admin.business.model.query.SubtitleStyleQuery;
import top.continew.admin.business.model.req.BatchReq;
import top.continew.admin.business.model.req.SubtitleStyleReq;
import top.continew.admin.business.model.resp.SubtitleStyleDetailResp;
import top.continew.admin.business.model.resp.SubtitleStyleResp;
import top.continew.admin.business.service.SubtitleStyleService;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.log.core.annotation.Log;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数字人-字幕样式管理 API
 *
 * @author 王志维
 * @since 2025/12/29
 */
@Tag(name = "数字人-字幕样式管理 API")
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/business/subtitleStyle", api = {Api.GET, Api.LIST})
public class SubtitleStyleController extends BaseController<SubtitleStyleService, SubtitleStyleResp, SubtitleStyleDetailResp, SubtitleStyleQuery, SubtitleStyleReq> {

    @Log
    @Operation(summary = "更新字幕样式", description = "更新字幕样式信息")
    @PutMapping("/{id}")
    public Boolean updateSubtitleStyle(@RequestBody SubtitleStyleReq req, @PathVariable Long id) {
        baseService.update(req, id);
        return true;
    }

    @Log
    @Operation(summary = "查询字幕样式", description = "查询字幕样式列表")
    @ResponseBody
    @GetMapping({"/list"})
    public List<SubtitleStyleResp> list(SubtitleStyleQuery query, SortQuery sortQuery) {
        return baseService.list(query, sortQuery);
    }

    @Log
    @ResponseBody
    @Operation(summary = "新增字幕样式", description = "新增字幕样式")
    @PostMapping()
    public Long addSubtitleStyle(@RequestBody SubtitleStyleReq req) {
        return baseService.add(req);
    }

    @Log
    @Operation(summary = "分页查询字幕样式", description = "分页查询字幕样式列表")
    @ResponseBody
    @GetMapping({"/page"})
    public PageResp<SubtitleStyleResp> page(SubtitleStyleQuery query, SubtitleStylePageQuery pageQuery) {
        return baseService.pageList(query, pageQuery);
    }

    @Log
    @Operation(summary = "批量删除字幕样式", description = "根据字幕样式ID列表批量删除字幕样式")
    @PostMapping("/batchDelete")
    public void deleteBatch(@Validated @RequestBody BatchReq req) {
        baseService.delete(req.getIds());
    }

    @Log
    @Operation(summary = "批量删除字幕样式（通过路径参数）", description = "根据路径中的ID列表批量删除字幕样式，ID以逗号分隔")
    @DeleteMapping("/{ids}")
    public void delete(@PathVariable("ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
        baseService.delete(idList);
    }
}