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.CopyrightPageQuery;
import top.continew.admin.business.model.query.CopyrightQuery;
import top.continew.admin.business.model.req.BatchReq;
import top.continew.admin.business.model.req.CopyrightReq;
import top.continew.admin.business.model.resp.CopyrightDetailResp;
import top.continew.admin.business.model.resp.CopyrightResp;
import top.continew.admin.business.service.CopyrightService;
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/copyright", api = {Api.GET, Api.UPDATE, Api.LIST})
public class CopyrightController extends BaseController<CopyrightService, CopyrightResp, CopyrightDetailResp, CopyrightQuery, CopyrightReq> {

    @Log
    @Operation(summary = "查询文案", description = "查询文案列表")
    @ResponseBody
    @GetMapping({"/list"})
    public List<CopyrightResp> list(CopyrightQuery query, SortQuery sortQuery) {
        return baseService.list(query, sortQuery);
    }

    @Log
    @ResponseBody
    @Operation(summary = "新增文案", description = "新增文案")
    @PostMapping()
    public Long addCopyright(@RequestBody CopyrightReq req) {
        return baseService.add(req);
    }

    @Log
    @ResponseBody
    @Operation(summary = "批量新增文案", description = "批量新增文案")
    @PostMapping("/batchAdd")
    public List<Long> batchAddCopyright(@RequestBody List<CopyrightReq> reqList) {
        return baseService.batchAdd(reqList);
    }

    @Log
    @Operation(summary = "分页查询文案", description = "分页查询文案列表")
    @ResponseBody
    @GetMapping({"/page"})
    public PageResp<CopyrightResp> page(CopyrightQuery query, CopyrightPageQuery 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);
    }
}