package com.qqchen.deploy.backend.system.api;

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.system.dto.SystemReleaseDTO;
import com.qqchen.deploy.backend.system.entity.SystemRelease;
import com.qqchen.deploy.backend.system.query.SystemReleaseQuery;
import com.qqchen.deploy.backend.system.service.ISystemReleaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 系统版本发布记录 Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/system-release")
@Tag(name = "系统版本发布管理", description = "系统版本发布记录管理相关接口")
public class SystemReleaseApiController
        extends BaseController<SystemRelease, SystemReleaseDTO, Long, SystemReleaseQuery> {

    @Resource
    private ISystemReleaseService systemReleaseService;

    @Override
    @PostMapping
    @Operation(summary = "创建发布记录", description = "创建新的版本发布记录")
    public Response<SystemReleaseDTO> create(@Validated @RequestBody SystemReleaseDTO dto) {
        return super.create(dto);
    }

    @Override
    @PutMapping("/{id}")
    @Operation(summary = "更新发布记录", description = "更新指定ID的版本发布记录")
    public Response<SystemReleaseDTO> update(@PathVariable Long id, @Validated @RequestBody SystemReleaseDTO dto) {
        return super.update(id, dto);
    }

    @Override
    @DeleteMapping("/{id}")
    @Operation(summary = "删除发布记录", description = "删除指定ID的版本发布记录（逻辑删除）")
    public Response<Void> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @Override
    @GetMapping("/{id}")
    @Operation(summary = "查询发布记录详情", description = "根据ID查询版本发布记录详情")
    public Response<SystemReleaseDTO> findById(@PathVariable Long id) {
        return super.findById(id);
    }

    @Override
    @GetMapping("/list")
    @Operation(summary = "查询所有发布记录", description = "查询所有版本发布记录列表")
    public Response<List<SystemReleaseDTO>> findAll() {
        return super.findAll();
    }

    @Override
    @GetMapping("/page")
    @Operation(summary = "分页查询发布记录", description = "分页查询版本发布记录")
    public Response<Page<SystemReleaseDTO>> page(SystemReleaseQuery query) {
        return super.page(query);
    }

    @Override
    @GetMapping("/query")
    @Operation(summary = "条件查询发布记录", description = "根据条件查询版本发布记录列表")
    public Response<List<SystemReleaseDTO>> findAll(SystemReleaseQuery query) {
        return super.findAll(query);
    }

    @Override
    @PostMapping("/batch")
    @Operation(summary = "批量处理发布记录", description = "批量创建/更新版本发布记录")
    public CompletableFuture<Response<Void>> batchProcess(@RequestBody List<SystemReleaseDTO> dtos) {
        return super.batchProcess(dtos);
    }

    /**
     * 获取未通知的发布记录列表
     */
    @GetMapping("/unnotified")
    @Operation(summary = "获取未通知的发布记录", description = "查询所有未发送通知的版本发布记录")
    public Response<List<SystemReleaseDTO>> getUnnotifiedReleases() {
        List<SystemReleaseDTO> releases = systemReleaseService.getUnnotifiedReleases();
        return Response.success(releases);
    }

    /**
     * 标记为已通知
     */
    @PutMapping("/{id}/notify")
    @Operation(summary = "标记为已通知", description = "将指定发布记录标记为已发送通知")
    public Response<Void> markAsNotified(@PathVariable Long id) {
        systemReleaseService.markAsNotified(id);
        return Response.success();
    }

    /**
     * 获取指定模块的最新发布记录
     */
    @GetMapping("/latest/{module}")
    @Operation(summary = "获取最新发布记录", description = "获取指定模块的最新版本发布记录")
    public Response<SystemReleaseDTO> getLatestReleaseByModule(@PathVariable String module) {
        SystemReleaseDTO release = systemReleaseService.getLatestReleaseByModule(module);
        return Response.success(release);
    }

    @Override
    protected void exportData(HttpServletResponse response, List<SystemReleaseDTO> data) {
        log.info("导出系统版本发布记录数据，数据量：{}", data.size());
    }
}
