package com.pjilisense.flxai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pjilisense.flxai.base.service.impl.CrudServiceImpl;
import com.pjilisense.flxai.dao.AppAiModelDao;
import com.pjilisense.flxai.dto.AppAiModelDTO;
import com.pjilisense.flxai.entity.AppAiModelEntity;
import com.pjilisense.flxai.entity.AppAiPluginEntity;
import com.pjilisense.flxai.service.AppAiModelService;
import cn.hutool.core.util.StrUtil;
import com.pjilisense.flxai.utils.MapUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * AI模型表
 *
 * @author liushujing liushujing@philisense.com
 * @since 1.0.0 2024-12-20
 */
@Service
public class AppAiModelServiceImpl extends CrudServiceImpl<AppAiModelDao, AppAiModelEntity, AppAiModelDTO> implements AppAiModelService {

    @Override
    public QueryWrapper<AppAiModelEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AppAiModelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    public QueryWrapper<AppAiModelEntity> getWrapperAll(Map<String, Object> params){
        String userid = (String)params.get("userid");

        QueryWrapper<AppAiModelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(userid), "userid", userid);
        wrapper.orderByDesc("created_at");
        return wrapper;
    }

    @Override
    public int deleteByIdentifier(String userid, String identifier) {
        QueryWrapper<AppAiModelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(identifier), "identifier", identifier);
        wrapper.eq(StrUtil.isNotBlank(userid), "userid", userid);
        return baseDao.delete(wrapper);
    }

    @Override
    public List<Map<String, Object>> getAllAiModel(String userid) {
        Map<String, Object> params =new HashMap<>();
        params.put("userid",userid);
        List<AppAiModelEntity> entityList = baseDao.selectList(getWrapperAll(params));
        params.clear();
        List<Map<String, Object>> retList = new ArrayList<>();
        if(entityList!=null && entityList.size()>0) {
            for (AppAiModelEntity entity : entityList) {
                Map<String, Object> mapx = MapUtils.objectToMap(entity);
                retList.add(mapx);
                HashMap<String, Object> meta = new HashMap<>();
                mapx.put("meta", meta);
                meta.put("displayname", mapx.get("displayname"));
                meta.put("description", mapx.get("description"));
                meta.put("title", mapx.get("title"));
                meta.put("enabled", mapx.get("enabled"));
                meta.put("category", mapx.get("category"));
                meta.put("id", mapx.get("metaId"));
                meta.put("tokens", mapx.get("metaTokens"));
                mapx.remove("displayname");
                mapx.remove("description");
                mapx.remove("title");
                mapx.remove("enabled");
                mapx.remove("category");
                mapx.remove("metaId");
                mapx.remove("metaTokens");
                String providers = (String) mapx.get("providers");
                if (providers!=null && providers.length() > 0) {
                    mapx.put("providers", Arrays.asList(providers.split("\\,")));
                } else {
                    mapx.put("providers", new ArrayList<>());
                }
                HashMap<String, Object> socialData = new HashMap<>();
                mapx.put("socialData", socialData);
                socialData.put("conversations", mapx.get("conversations"));
                socialData.put("likes", mapx.get("likes"));
                socialData.put("tokens", mapx.get("tokens"));
                mapx.remove("conversations");
                mapx.remove("likes");
                mapx.remove("tokens");
                String suggestions = (String) mapx.get("suggestions");
                if (suggestions!=null && suggestions.length() > 0) {
                    mapx.put("suggestions", Arrays.asList(suggestions.split("\\,")));
                } else {
                    mapx.put("suggestions", new ArrayList<>());
                }
            }
        }
        return retList;
    }
}