package com.devjob.application.client;

import com.devjob.application.dto.JobInfo;
import com.devjob.common.dto.ApiResponse;
import com.devjob.common.exception.BusinessException;
import com.devjob.common.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientResponseException;

@Component
@RequiredArgsConstructor
public class JobClient {

    private final JobHttpClient jobHttpClient;

    // 회복성(재시도·벌크헤드)은 JobHttpClient에 있다. 이 래퍼는 응답을 풀고 404를 도메인 예외로 바꾼다.
    public JobInfo getJob(Long jobId) {
        try {
            ApiResponse<JobInfo> response = jobHttpClient.getJob(jobId);
            if (response == null || response.data() == null) {
                throw new BusinessException(ErrorCode.JOB_NOT_FOUND);
            }
            return response.data();
        } catch (RestClientResponseException e) {
            // 공고 서비스가 404 → 없는 공고를 깔끔한 JOB_NOT_FOUND로 변환
            if (e.getStatusCode().value() == 404) {
                throw new BusinessException(ErrorCode.JOB_NOT_FOUND);
            }
            throw e;
        }
    }
}
