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 org.springframework.web.multipart.MultipartFile;
import top.continew.admin.business.model.query.EffectTemplatePageQuery;
import top.continew.admin.business.model.query.EffectTemplateQuery;
import top.continew.admin.business.model.req.BatchReq;
import top.continew.admin.business.model.req.EffectTemplateReq;
import top.continew.admin.business.model.resp.EffectTemplateDetailResp;
import top.continew.admin.business.model.resp.EffectTemplateResp;
import top.continew.admin.business.service.EffectTemplateService;
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.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/effectTemplate", api = {Api.GET, Api.LIST})
public class EffectTemplateController extends BaseController<EffectTemplateService, EffectTemplateResp, EffectTemplateDetailResp, EffectTemplateQuery, EffectTemplateReq> {

    @Log
    @Operation(summary = "更新特效模板", description = "更新特效模板信息")
    @PutMapping("/{id}")
    public Boolean updateEffectTemplate(@RequestBody EffectTemplateReq req, @PathVariable Long id) {
        baseService.update(req, id);
        return true;
    }

    @Log
    @ResponseBody
    @Operation(summary = "新增特效模板", description = "新增特效模板")
    @PostMapping()
    public Long addEffectTemplate(@RequestBody EffectTemplateReq req) {
        return baseService.add(req);
    }

    @Log
    @Operation(summary = "分页查询特效模板", description = "分页查询特效模板列表")
    @ResponseBody
    @GetMapping({"/page"})
    public PageResp<EffectTemplateResp> page(EffectTemplateQuery query, EffectTemplatePageQuery 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);
    }

    @Log
    @Operation(summary = "上传特效模板", description = "上传特效模板文件")
    @PostMapping("/uploadTemplate")
    public Long uploadTemplate(@RequestParam("file") MultipartFile file,
                               @RequestParam(value = "name", required = false) String name,
                               @RequestParam(value = "type", required = false) String type,
                               @RequestParam(value = "userId", required = false) String userId) {
        return baseService.uploadTemplate(file, name, type, userId);
    }
}