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;

    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로 변환(RestClient는 4xx에서 예외를 던진다)
            if (e.getStatusCode().value() == 404) {
                throw new BusinessException(ErrorCode.JOB_NOT_FOUND);
            }
            throw e;
        }
    }
}
