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

import com.yolo.keyboard.dal.dataobject.tag.TagI18nDO;
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.tag.vo.*;
import com.yolo.keyboard.dal.dataobject.tag.KeyboardTagDO;
import com.yolo.keyboard.service.tag.KeyboardTagService;

@Tag(name = "管理后台 - 人设标签")
@RestController
@RequestMapping("/keyboard/tag")
@Validated
public class KeyboardTagController {

    @Resource
    private KeyboardTagService tagService;

    @PostMapping("/create")
    @Operation(summary = "创建人设标签")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:create')")
    public CommonResult<Integer> createTag(@Valid @RequestBody KeyboardTagSaveReqVO createReqVO) {
        return success(tagService.createTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新人设标签")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:update')")
    public CommonResult<Boolean> updateTag(@Valid @RequestBody KeyboardTagSaveReqVO updateReqVO) {
        tagService.updateTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除人设标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:tag:delete')")
    public CommonResult<Boolean> deleteTag(@RequestParam("id") Integer id) {
        tagService.deleteTag(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除人设标签")
                @PreAuthorize("@ss.hasPermission('keyboard:tag:delete')")
    public CommonResult<Boolean> deleteTagList(@RequestParam("ids") List<Integer> ids) {
        tagService.deleteTagListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得人设标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:query')")
    public CommonResult<KeyboardTagRespVO> getTag(@RequestParam("id") Integer id) {
        KeyboardTagDO tag = tagService.getTag(id);
        return success(BeanUtils.toBean(tag, KeyboardTagRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得人设标签分页")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:query')")
    public CommonResult<PageResult<KeyboardTagRespVO>> getTagPage(@Valid KeyboardTagPageReqVO pageReqVO) {
        PageResult<KeyboardTagDO> pageResult = tagService.getTagPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardTagRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出人设标签 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTagExcel(@Valid KeyboardTagPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardTagDO> list = tagService.getTagPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "人设标签.xls", "数据", KeyboardTagRespVO.class,
                        BeanUtils.toBean(list, KeyboardTagRespVO.class));
    }
    // ==================== 子表（人设标签国际化） ====================

    @GetMapping("/tag-i18n/page")
    @Operation(summary = "获得人设标签国际化分页")
    @Parameter(name = "tagId", description = "标签主表ID，对应 keyboard_tag.id")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:query')")
    public CommonResult<PageResult<TagI18nDO>> getTagI18nPage(PageParam pageReqVO,
                                                              @RequestParam("tagId") Integer tagId) {
        return success(tagService.getTagI18nPage(pageReqVO, tagId));
    }

    @PostMapping("/tag-i18n/create")
    @Operation(summary = "创建人设标签国际化")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:create')")
    public CommonResult<Long> createTagI18n(@Valid @RequestBody TagI18nDO tagI18n) {
        return success(tagService.createTagI18n(tagI18n));
    }

    @PutMapping("/tag-i18n/update")
    @Operation(summary = "更新人设标签国际化")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:update')")
    public CommonResult<Boolean> updateTagI18n(@Valid @RequestBody TagI18nDO tagI18n) {
        tagService.updateTagI18n(tagI18n);
        return success(true);
    }

    @DeleteMapping("/tag-i18n/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除人设标签国际化")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:delete')")
    public CommonResult<Boolean> deleteTagI18n(@RequestParam("id") Long id) {
        tagService.deleteTagI18n(id);
        return success(true);
    }

    @DeleteMapping("/tag-i18n/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除人设标签国际化")
    @PreAuthorize("@ss.hasPermission('keyboard:tag:delete')")
    public CommonResult<Boolean> deleteTagI18nList(@RequestParam("ids") List<Long> ids) {
        tagService.deleteTagI18nListByIds(ids);
        return success(true);
    }

    @GetMapping("/tag-i18n/get")
    @Operation(summary = "获得人设标签国际化")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:tag:query')")
    public CommonResult<TagI18nDO> getTagI18n(@RequestParam("id") Long id) {
        return success(tagService.getTagI18n(id));
    }
}