package cn.iocoder.byzs.module.ai.controller.admin.mapgame;

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 cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;

import cn.iocoder.byzs.framework.excel.core.util.ExcelUtils;

import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.byzs.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.byzs.module.ai.controller.admin.mapgame.vo.*;
import cn.iocoder.byzs.module.ai.dal.dataobject.mapgame.MapGameDO;
import cn.iocoder.byzs.module.ai.service.mapgame.MapGameService;

@Tag(name = "管理后台 - AI-blockly地图编程游戏")
@RestController
@RequestMapping("/ai/map-game")
@Validated
public class MapGameController {

    @Resource
    private MapGameService mapGameService;

    @PostMapping("/create")
    @Operation(summary = "创建AI-blockly地图编程游戏")
    @PreAuthorize("@ss.hasPermission('ai:map-game:create')")
    public CommonResult<Integer> createMapGame(@Valid @RequestBody MapGameSaveReqVO createReqVO) {
        return success(mapGameService.createMapGame(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI-blockly地图编程游戏")
    @PreAuthorize("@ss.hasPermission('ai:map-game:update')")
    public CommonResult<Boolean> updateMapGame(@Valid @RequestBody MapGameSaveReqVO updateReqVO) {
        mapGameService.updateMapGame(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI-blockly地图编程游戏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:map-game:delete')")
    public CommonResult<Boolean> deleteMapGame(@RequestParam("id") Integer id) {
        mapGameService.deleteMapGame(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI-blockly地图编程游戏")
                @PreAuthorize("@ss.hasPermission('ai:map-game:delete')")
    public CommonResult<Boolean> deleteMapGameList(@RequestParam("ids") List<Integer> ids) {
        mapGameService.deleteMapGameListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI-blockly地图编程游戏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:map-game:query')")
    public CommonResult<MapGameRespVO> getMapGame(@RequestParam("id") Integer id) {
        MapGameDO mapGame = mapGameService.getMapGame(id);
        return success(BeanUtils.toBean(mapGame, MapGameRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI-blockly地图编程游戏分页")
    @PreAuthorize("@ss.hasPermission('ai:map-game:query')")
    public CommonResult<PageResult<MapGameRespVO>> getMapGamePage(@Valid MapGamePageReqVO pageReqVO) {
        PageResult<MapGameDO> pageResult = mapGameService.getMapGamePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MapGameRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI-blockly地图编程游戏 Excel")
    @PreAuthorize("@ss.hasPermission('ai:map-game:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMapGameExcel(@Valid MapGamePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MapGameDO> list = mapGameService.getMapGamePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI-blockly地图编程游戏.xls", "数据", MapGameRespVO.class,
                        BeanUtils.toBean(list, MapGameRespVO.class));
    }

}