/*
 * Copyright (C) 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#ifdef TRITON_CLIENT
#include <grpc_client.h>
#endif

#include <memory>
#include <string>
#include <opencv4/opencv2/opencv.hpp>

namespace ai_engine::core_ai::embedding {

enum ERROR_CODE : int {
    SYSTEM_ERR = -1,
    SUCCESS = 0,
    LOAD_MODEL_ERROR,
    CREATE_TRITON_CLIENT_ERROR,
    TRITON_INFER_ERROR,
    ADD_EMBEDDING_TASK_ERROR,
};
typedef struct EmbeddingProxyError {
    int code;
    std::string message;
} EmbeddingProxyError;

typedef struct EmbeddingProxyResult {
    bool success;
    EmbeddingProxyError error;
    std::vector<float> res;
    EmbeddingProxyResult() {}
    EmbeddingProxyResult(bool success, std::vector<float> res)
        : success(success), res(res), error{SUCCESS, "Success"} {}

    EmbeddingProxyResult(bool success, int code, std::string message)
        : success(success), res({}), error({code, message}) {}

} EmbeddingProxyResult;
class EmbeddingProxy {
public:
    EmbeddingProxy();
    bool createTritonClient();
    EmbeddingProxyResult loadTextModel();
    EmbeddingProxyResult loadImageModel();

    std::string modelInfo() const;
    EmbeddingProxyResult embeddingText(const std::string &text);
    EmbeddingProxyResult embeddingImage(
        const std::string &fileData,
        int fileDataType); // dataType
                           // 区分图片数据类型，0:输入为图片路径，1：输入base64图片格式
    EmbeddingProxyResult embeddingTextWithImageModel(const std::string &text);

private:
    // int modelSwitch
    // 由于文本和图像模型对于输出shape的检查方式不同，所以用这个参数区分一下
    // modelSwitch 0:text model , 1: image model
#ifdef TRITON_CLIENT
    bool validateShapeAndDatatype(
        const std::string &name,
        std::shared_ptr<triton::client::InferResult> result,
        const std::string &expectedDataType, const int modelSwitch);
#endif
    std::vector<unsigned char> base64Decode(const std::string &input);
    std::vector<float> imageToFloatVector(const cv::Mat &image);
    std::vector<float> preprocessVision(cv::Mat &image);

private:
    bool initTritonClient_ = false;
    bool initTextModel_ = false;
    bool initImageModel_ = false;

private:
    const std::string textEmbeddingModel_ = "ensemble-embd_gte-base_uint8-text";
    const std::string textEmbeddingVersion_ = "";
    const int textModelDim_ = 768;

    const std::string textEmbeddingImageModel_ =
        "ensemble-embd_cn-clip_512-uint8-text";
    const std::string textEmbeddingImageVersion_ = "";
    const std::string imageEmbeddingModel_ = "embd_cn-clip_512-uint8-image";
    const std::string imageEmbeddingVersion_ = "";
    const int imageModelDim_ = 1024;

    const std::string grpcServerUrl_ = "localhost:8001";
#ifdef TRITON_CLIENT
    std::unique_ptr<triton::client::InferenceServerGrpcClient> grpcClient_;
#endif
};

} // namespace ai_engine::core_ai::embedding