package edu.whut.config;

import edu.whut.infrastructure.gateway.IGroupBuyMarketService;
import edu.whut.infrastructure.gateway.IWeixinApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

@Slf4j
@Configuration
public class Retrofit2Config {

    // 微信开放平台的基础 URL，后续所有接口都会在这个前缀下拼接路径
    private static final String BASE_URL = "https://api.weixin.qq.com/";

    @Value("${app.config.group-buy-market.api-url}")
    private String groupBuyMarketApiUrl;


    /**
     * 通过 Retrofit 动态生成 IWeixinApiService 接口的实现，
     * 并注册为 Spring 容器中的 Bean，方便业务层直接注入使用
     */
    @Bean
    public IWeixinApiService weixinApiService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(JacksonConverterFactory.create()).build();
        return retrofit.create(IWeixinApiService.class);
    }

    @Bean
    public IGroupBuyMarketService groupBuyMarketService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(groupBuyMarketApiUrl)
                .addConverterFactory(JacksonConverterFactory.create()).build();

        return retrofit.create(IGroupBuyMarketService.class);
    }

}
