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

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.appversions.vo.*;
import com.yolo.keyboard.dal.dataobject.appversions.AppVersionsDO;
import com.yolo.keyboard.service.appversions.AppVersionsService;

@Tag(name = "管理后台 - App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。")
@RestController
@RequestMapping("/keyboard/app-versions")
@Validated
public class AppVersionsController {

    @Resource
    private AppVersionsService appVersionsService;

    @PostMapping("/create")
    @Operation(summary = "创建App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。")
    @PreAuthorize("@ss.hasPermission('keyboard:app-versions:create')")
    public CommonResult<Long> createAppVersions(@RequestBody AppVersionsSaveReqVO createReqVO) {
        return success(appVersionsService.createAppVersions(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。")
    @PreAuthorize("@ss.hasPermission('keyboard:app-versions:update')")
    public CommonResult<Boolean> updateAppVersions(@RequestBody AppVersionsSaveReqVO updateReqVO) {
        appVersionsService.updateAppVersions(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:app-versions:delete')")
    public CommonResult<Boolean> deleteAppVersions(@RequestParam("id") Long id) {
        appVersionsService.deleteAppVersions(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。")
                @PreAuthorize("@ss.hasPermission('keyboard:app-versions:delete')")
    public CommonResult<Boolean> deleteAppVersionsList(@RequestParam("ids") List<Long> ids) {
        appVersionsService.deleteAppVersionsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:app-versions:query')")
    public CommonResult<AppVersionsRespVO> getAppVersions(@RequestParam("id") Long id) {
        AppVersionsDO appVersions = appVersionsService.getAppVersions(id);
        return success(BeanUtils.toBean(appVersions, AppVersionsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。分页")
    @PreAuthorize("@ss.hasPermission('keyboard:app-versions:query')")
    public CommonResult<PageResult<AppVersionsRespVO>> getAppVersionsPage(@Valid AppVersionsPageReqVO pageReqVO) {
        PageResult<AppVersionsDO> pageResult = appVersionsService.getAppVersionsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppVersionsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:app-versions:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppVersionsExcel(@Valid AppVersionsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppVersionsDO> list = appVersionsService.getAppVersionsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。.xls", "数据", AppVersionsRespVO.class,
                        BeanUtils.toBean(list, AppVersionsRespVO.class));
    }

}