package ru.soune.nocopy.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import ru.soune.nocopy.dto.BaseRequest;
import ru.soune.nocopy.dto.BaseResponse;
import ru.soune.nocopy.dto.MessageCode;
import ru.soune.nocopy.dto.dadata.DaDataRequest;
import ru.soune.nocopy.dto.dadata.DaDataResponse;
import ru.soune.nocopy.service.dadata.DaDataService;

@Component
@RequiredArgsConstructor
@Slf4j
public class DaDataHandler  implements RequestHandler  {

    private final DaDataService daDataService;

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public BaseResponse handle(BaseRequest request) throws Exception {
        DaDataRequest daDataRequest = mapper.convertValue(request.getMessageBody(), DaDataRequest.class);
        String inn = daDataRequest.getInn();
        DaDataResponse daData = daDataService.callDaData(inn);

        return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
                MessageCode.SUCCESS.getDescription(), daData);
    }
}
