package com.zioinfo.esn.controller;

import com.zioinfo.esn.common.ApiResponse;
import com.zioinfo.esn.domain.AlarmVo;
import com.zioinfo.esn.service.AlarmService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/alarms")
@RequiredArgsConstructor
public class AlarmController {
    private final AlarmService service;

    @GetMapping
    public ApiResponse<List<AlarmVo>> list(
            @RequestParam(required = false) String tenantCode,
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) String severity,
            @RequestParam(required = false) String status) {
        return ApiResponse.ok(service.list(tenantCode, storeId, severity, status));
    }

    @GetMapping("/{id}")
    public ApiResponse<AlarmVo> get(@PathVariable Long id) {
        return ApiResponse.ok(service.get(id));
    }

    @PostMapping
    public ApiResponse<AlarmVo> create(@RequestBody AlarmVo v) {
        return ApiResponse.ok("알람 생성 완료", service.create(v));
    }

    @PutMapping("/{id}")
    public ApiResponse<AlarmVo> update(@PathVariable Long id, @RequestBody AlarmVo v) {
        return ApiResponse.ok("알람 수정 완료", service.update(id, v));
    }

    @PutMapping("/{id}/resolve")
    public ApiResponse<Void> resolve(@PathVariable Long id,
                                      @RequestBody Map<String, String> body,
                                      Authentication auth) {
        String resolvedBy = auth != null ? auth.getName() : "system";
        service.resolve(id, resolvedBy, body.getOrDefault("resolution", ""));
        return ApiResponse.ok("알람 해결 완료", null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return ApiResponse.ok("알람 삭제 완료", null);
    }
}
