package com.tzld.piaoquan.recommend.server.service.rank.tansform;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Getter
public class FGEncoderManager {

    private static final FGEncoderManager instance = new FGEncoderManager();
    private final Map<String, FGEncoder> featureFGEncoderMap = new HashMap<>();

    private final OSS client;
    private final String bucketName;

    private FGEncoderManager() {
        // config load
        Config config = ConfigService.getAppConfig();
        String endpoint = config.getProperty("model.oss.internal.endpoint", "");
        String accessKeyId = config.getProperty("model.oss.accessKeyId", "");
        String accessKetSecret = config.getProperty("model.oss.accessKetSecret", "");
        // oss client
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKetSecret);
        this.client = new OSSClientBuilder().build(endpoint, credentialsProvider);
        this.bucketName = config.getProperty("model.oss.bucketName", "");

        this.registerAllFGEncoder();
    }

    public static FGEncoderManager getInstance() {
        return instance;
    }

    public void registerAllFGEncoder() {
        this.registerFGEncoder("zhaohaipeng/pai/config/fg/feature_list_20260403.json");
    }

    public void registerFGEncoder(final String fgConfigJsonPath) {
        FGEncoder fgEncoder = new FGEncoder(client.getObject(this.bucketName, fgConfigJsonPath).getObjectContent());
        this.featureFGEncoderMap.put(fgConfigJsonPath, fgEncoder);
    }

    public FGEncoder getFGEncoder(final String fgConfigJsonPath) {
        return this.featureFGEncoderMap.get(fgConfigJsonPath);
    }
}
