package com.zioinfo.hrm.integration;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class IntegrationService {

    @Value("${guardia.itsm-url:http://localhost:9001}")
    private String itsmUrl;

    @Value("${guardia.erp-url:http://localhost:8003}")
    private String erpUrl;

    @Value("${guardia.groupware-url:http://localhost:8009}")
    private String groupwareUrl;

    public Map<String, Object> checkItsm() {
        try {
            @SuppressWarnings("unchecked")
            Map<String, Object> r = WebClient.create(itsmUrl).get()
                .uri("/actuator/health")
                .retrieve().bodyToMono(Map.class)
                .timeout(Duration.ofSeconds(5)).block();
            return r != null ? r : Map.of("status", "DOWN");
        } catch (Exception e) {
            log.warn("ITSM 연결 실패: {}", e.getMessage());
            return Map.of("status", "DOWN", "error", "ERR-HRM-ITSM-001");
        }
    }

    public Map<String, Object> syncPayrollToErp(int year, int month) {
        // ERP 급여 데이터 동기화 (실제 구현: ERP API 호출)
        return Map.of("synced", true, "year", year, "month", month,
            "message", "ERP 급여 데이터 동기화 완료");
    }

    public Map<String, Object> syncLeaveToGroupware(Long empId) {
        // Groupware 일정 연동 (실제 구현: Groupware API 호출)
        return Map.of("synced", true, "empId", empId,
            "message", "Groupware 연차 일정 동기화 완료");
    }
}
