package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.RiskAlertService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/risk-alert")
@RequiredArgsConstructor
@Slf4j
public class RiskAlertController {

    private final RiskAlertService riskAlertService;

    /** GET / → 전체 알림 목록 */
    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> getAllAlerts(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(riskAlertService.getAllAlerts(params)));
    }

    /** POST /refresh → 알림 새로고침 */
    @PostMapping("/refresh")
    public ResponseEntity<ApiResponse<Map<String, Object>>> refreshAlerts(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody(required = false) Map<String, Object> body) {
        Map<String, Object> params = body != null ? new HashMap<>(body) : new HashMap<>();
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(riskAlertService.refreshAlerts(params)));
    }

    /** GET /weather → 기상 알림 목록 */
    @GetMapping("/weather")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getWeatherAlerts(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(riskAlertService.getWeatherAlerts(params)));
    }

    /** GET /accidents → 사고 알림 목록 */
    @GetMapping("/accidents")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getAccidentAlerts(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(riskAlertService.getAccidentAlerts(params)));
    }

    /** GET /roadworks → 공사 알림 목록 */
    @GetMapping("/roadworks")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getRoadworkAlerts(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(riskAlertService.getRoadworkAlerts(params)));
    }
}
