package com.yolo.keyboard.controller.admin.aicompanion;

import com.yolo.keyboard.dal.dataobject.aicompanion.AiCompanionI18nDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yolo.keyboard.controller.admin.aicompanion.vo.*;
import com.yolo.keyboard.dal.dataobject.aicompanion.KeyboardAiCompanionDO;
import com.yolo.keyboard.service.aicompanion.KeyboardAiCompanionService;

@Tag(name = "管理后台 - AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设")
@RestController
@RequestMapping("/keyboard/ai-companion")
@Validated
public class KeyboardAiCompanionController {

    @Resource
    private KeyboardAiCompanionService aiCompanionService;

    @PostMapping("/create")
    @Operation(summary = "创建AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:create')")
    public CommonResult<Long> createAiCompanion(@Valid @RequestBody KeyboardAiCompanionSaveReqVO createReqVO) {
        return success(aiCompanionService.createAiCompanion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:update')")
    public CommonResult<Boolean> updateAiCompanion(@Valid @RequestBody KeyboardAiCompanionSaveReqVO updateReqVO) {
        aiCompanionService.updateAiCompanion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:delete')")
    public CommonResult<Boolean> deleteAiCompanion(@RequestParam("id") Long id) {
        aiCompanionService.deleteAiCompanion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设")
                @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:delete')")
    public CommonResult<Boolean> deleteAiCompanionList(@RequestParam("ids") List<Long> ids) {
        aiCompanionService.deleteAiCompanionListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:query')")
    public CommonResult<KeyboardAiCompanionRespVO> getAiCompanion(@RequestParam("id") Long id) {
        KeyboardAiCompanionDO aiCompanion = aiCompanionService.getAiCompanion(id);
        return success(BeanUtils.toBean(aiCompanion, KeyboardAiCompanionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设分页")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:query')")
    public CommonResult<PageResult<KeyboardAiCompanionRespVO>> getAiCompanionPage(@Valid KeyboardAiCompanionPageReqVO pageReqVO) {
        PageResult<KeyboardAiCompanionDO> pageResult = aiCompanionService.getAiCompanionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardAiCompanionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAiCompanionExcel(@Valid KeyboardAiCompanionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardAiCompanionDO> list = aiCompanionService.getAiCompanionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设.xls", "数据", KeyboardAiCompanionRespVO.class,
                        BeanUtils.toBean(list, KeyboardAiCompanionRespVO.class));
    }

    // ==================== 子表（AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍） ====================

    @GetMapping("/ai-companion-i18n/page")
    @Operation(summary = "获得AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍分页")
    @Parameter(name = "id", description = "主键ID")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:query')")
    public CommonResult<PageResult<AiCompanionI18nDO>> getAiCompanionI18nPage(PageParam pageReqVO,
                                                                              @RequestParam("id") Long id) {
        return success(aiCompanionService.getAiCompanionI18nPage(pageReqVO, id));
    }

    @PostMapping("/ai-companion-i18n/create")
    @Operation(summary = "创建AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:create')")
    public CommonResult<Long> createAiCompanionI18n(@Valid @RequestBody AiCompanionI18nDO aiCompanionI18n) {
        return success(aiCompanionService.createAiCompanionI18n(aiCompanionI18n));
    }

    @PutMapping("/ai-companion-i18n/update")
    @Operation(summary = "更新AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:update')")
    public CommonResult<Boolean> updateAiCompanionI18n(@Valid @RequestBody AiCompanionI18nDO aiCompanionI18n) {
        aiCompanionService.updateAiCompanionI18n(aiCompanionI18n);
        return success(true);
    }

    @DeleteMapping("/ai-companion-i18n/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:delete')")
    public CommonResult<Boolean> deleteAiCompanionI18n(@RequestParam("id") Long id) {
        aiCompanionService.deleteAiCompanionI18n(id);
        return success(true);
    }

    @DeleteMapping("/ai-companion-i18n/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍")
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:delete')")
    public CommonResult<Boolean> deleteAiCompanionI18nList(@RequestParam("ids") List<Long> ids) {
        aiCompanionService.deleteAiCompanionI18nListByIds(ids);
        return success(true);
    }

	@GetMapping("/ai-companion-i18n/get")
	@Operation(summary = "获得AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:ai-companion:query')")
	public CommonResult<AiCompanionI18nDO> getAiCompanionI18n(@RequestParam("id") Long id) {
	    return success(aiCompanionService.getAiCompanionI18n(id));
	}

}