package com.gameplatform.server.controller.admin;

import com.gameplatform.server.model.dto.cooldown.CooldownStatsResponse;
import com.gameplatform.server.model.dto.cooldown.MachineCooldownView;
import com.gameplatform.server.service.cooldown.MemoryMachineCooldownService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/admin/cooldown")
@Tag(name = "机器冷却", description = "查询与管理内存中的机器冷却状态")
public class CooldownController {

    private final MemoryMachineCooldownService machineCooldownService;

    public CooldownController(MemoryMachineCooldownService machineCooldownService) {
        this.machineCooldownService = machineCooldownService;
    }

    @GetMapping("/list")
    @Operation(summary = "列出冷却记录", description = "获取当前内存中的机器冷却记录")
    public ResponseEntity<List<MachineCooldownView>> listCooldowns(
            @Parameter(description = "仅返回仍在冷却中的记录")
            @RequestParam(name = "activeOnly", required = false, defaultValue = "true") boolean activeOnly
    ) {
        List<MachineCooldownView> list = machineCooldownService.listAllCooldowns(activeOnly);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/stats")
    @Operation(summary = "冷却统计", description = "获取冷却总数、活跃数与总剩余分钟数")
    public ResponseEntity<CooldownStatsResponse> getStats() {
        MemoryMachineCooldownService.CooldownStats stats = machineCooldownService.getCooldownStats();
        CooldownStatsResponse body = new CooldownStatsResponse(true,
                stats.getTotalDevices(),
                stats.getActiveDevices(),
                stats.getTotalRemainingMinutes());
        return ResponseEntity.ok(body);
    }
}


