package com.interplug.qcast.biz.storeFavorite;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.interplug.qcast.biz.storeFavorite.dto.StoreFavoriteRequest;
import com.interplug.qcast.biz.storeFavorite.dto.StoreFavoriteResponse;
import com.interplug.qcast.config.Exception.ErrorCode;
import com.interplug.qcast.config.Exception.QcastException;
import com.interplug.qcast.config.message.Messages;
import com.interplug.qcast.util.InterfaceQsp;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

@Slf4j
@Service
@RequiredArgsConstructor
public class StoreFavoriteService {
  private final StoreFavoriteMapper storeFavoriteMapper;

  private final InterfaceQsp interfaceQsp;

  @Autowired Messages message;

  @Value("${qsp.url}")
  private String QSP_API_URL;

  /**
   * 판매점 즐겨찾기 정보 저장
   *
   * @param req
   * @return
   * @throws Exception
   */
  public int setStoreFavoriteSave(StoreFavoriteRequest req) throws Exception {
    return storeFavoriteMapper.setStoreFavoriteSave(req);
  }

  /**
   * 판매점 즐겨찾기 목록 조회
   *
   * @param req
   * @return
   * @throws Exception
   */
  public StoreFavoriteResponse getStoreFavoriteList(StoreFavoriteRequest req) throws Exception {

    StoreFavoriteResponse response = null;

    /* [0]. Validation Check */
    if ("".equals(req.getUserId())) {
      // [msg] {0} is required input value.
      throw new QcastException(
          ErrorCode.INVALID_INPUT_VALUE,
          message.getMessage("common.message.required.data", "User Id"));
    }
    /* [1]. QSP API (url + param) Setting */
    String url = QSP_API_URL + "/api/user/storeFavoriteInfoList";
    String apiUrl =
        UriComponentsBuilder.fromHttpUrl(url)
            .queryParam("userId", req.getUserId())
            .build()
            .toUriString();

    /* [2]. QSP API CALL -> Response */
    String strResponse = interfaceQsp.callApi(HttpMethod.GET, apiUrl, null);

    if (!"".equals(strResponse)) {
      ObjectMapper om =
          new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      response = om.readValue(strResponse, StoreFavoriteResponse.class);
    } else {
      // [msg] No data
      throw new QcastException(ErrorCode.NOT_FOUND, message.getMessage("common.message.no.data"));
    }
    return response;
  }

  public void setStoreFavoriteBatch(List<StoreFavoriteRequest> storeFavList) throws Exception {
    // 즐겨찾기 동기화
    for (StoreFavoriteRequest storeFavoriteRequest : storeFavList) {
      try {
        storeFavoriteMapper.setStoreFavoriteSave(storeFavoriteRequest);
      } catch (Exception e) {
        log.error(e.getMessage());
      }
    }
  }
}
