package com.ga.api.controller.kpi;

import com.ga.api.service.kpi.RetentionAnomalyAlertService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.core.vo.kpi.RetentionAnomalyAlertResp;
import com.ga.core.vo.kpi.RetentionAnomalyAlertSaveReq;
import com.ga.core.vo.kpi.RetentionAnomalyAlertSearchParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "유지율 이상치")
@RestController
@RequestMapping("/api/retention-anomalies")
@RequiredArgsConstructor
public class RetentionAnomalyAlertController {

    private final RetentionAnomalyAlertService service;

    @GetMapping
    @Operation(summary = "이상치 목록")
    @RequirePermission(menu = "RETENTION_ANOMALY", perm = "READ")
    public ApiResponse<PageResponse<RetentionAnomalyAlertResp>> list(@ModelAttribute RetentionAnomalyAlertSearchParam p) {
        return ApiResponse.ok(service.list(p));
    }

    @GetMapping("/{id}")
    @Operation(summary = "이상치 단건 상세")
    @RequirePermission(menu = "RETENTION_ANOMALY", perm = "READ")
    public ApiResponse<RetentionAnomalyAlertResp> detail(@PathVariable Long id) {
        return ApiResponse.ok(service.detail(id));
    }

    @PostMapping("/detect")
    @Operation(summary = "이상치 재탐지 (월 단위)")
    @RequirePermission(menu = "RETENTION_ANOMALY", perm = "UPDATE")
    @DataChangeLog(menu = "RETENTION_ANOMALY", table = "retention_anomaly_alert")
    public ApiResponse<Integer> detect(@RequestBody Map<String, String> body) {
        return ApiResponse.ok(service.detect(body.get("alertMonth")));
    }

    @PutMapping("/review")
    @Operation(summary = "이상치 검토/완료 처리")
    @RequirePermission(menu = "RETENTION_ANOMALY", perm = "UPDATE")
    @DataChangeLog(menu = "RETENTION_ANOMALY", table = "retention_anomaly_alert")
    public ApiResponse<Void> review(@RequestBody RetentionAnomalyAlertSaveReq req) {
        service.review(req);
        return ApiResponse.ok();
    }
}
