package com.heyu.api.controller.translation;

import com.heyu.api.alibaba.LLMUtils;
import com.heyu.api.alibaba.request.common.text.TranslationRequest;
import com.heyu.api.alibaba.request.common.text.TranslationResp;
import com.heyu.api.alibaba.resp.ModelResult;
import com.heyu.api.data.annotation.EbAuthentication;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping("/general/translation")
public class TranslationController {

    // http://localhost:8888/general/translation/language?content=我是一只小猫&language=英语
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/language1000")
    public R language1000(@RequestBody TranslationRequest translationRequest) throws Exception {
        if(translationRequest.getContent().length() > 1000){
            return R.ok("字符长度不能超过1000");
        }
        return doTranslation(translationRequest);

    }

    // http://localhost:8888/general/translation/language?content=我是一只小猫&language=英语
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/language2000")
    public R language2000(@RequestBody TranslationRequest translationRequest) throws Exception {
        if(translationRequest.getContent().length() > 2000){
            return R.ok("字符长度不能超过2000");
        }
        return doTranslation(translationRequest);
    }

    // http://localhost:8888/general/translation/language?content=我是一只小猫&language=英语
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/language5000")
    public R language(@RequestBody TranslationRequest translationRequest) throws Exception {
        if(translationRequest.getContent().length() > 5000){
            return R.ok("字符长度不能超过5000");
        }
        return doTranslation(translationRequest);
    }

    // http://localhost:8888/general/translation/language?content=我是一只小猫&language=英语
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/language8000")
    public R language8000(@RequestBody TranslationRequest translationRequest) throws Exception {
        if(translationRequest.getContent().length() > 8000){
            return R.ok("字符长度不能超过8000");
        }
        return doTranslation(translationRequest);
    }

    public R doTranslation(@RequestBody TranslationRequest translationRequest) {
        String prompt = LLMUtils.prompt + translationRequest.getLanguage();

        ModelResult modelResult = LLMUtils.callBaiLian(translationRequest.getContent(), prompt);

        TranslationResp resp = new TranslationResp();

        resp.setLanguage(translationRequest.getLanguage());
        resp.setContent(translationRequest.getContent());

        if(modelResult!=null){
            resp.setTranslationContent(modelResult.getResult());
        }
        return R.ok().setData(resp);
    }




}