package org.wcs.serve.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.business.data.intf.IDataLoader;
import org.wcs.factory.AppServeResponseFactory;
import org.wcs.mapper.intf.AppBaseApiInfoService;
import org.wcs.model.dto.serve.api.AddApiBaseInfoReq;
import org.wcs.model.dto.serve.api.QueryApiBaseInfoReq;
import org.wcs.model.dto.serve.api.UpdateApiBaseInfoReq;
import org.wcs.model.po.app.AppBaseApiInfo;
import org.wcs.model.vo.serve.AppServeDataResponse;
import org.wcs.model.vo.serve.AppServeResponse;
import org.wcs.model.vo.serve.api.QueryApiBaseInfoVo;
import org.wcs.serve.service.intf.IApiService;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ApiService implements IApiService {

    private final AppBaseApiInfoService appBaseApiInfoService;
    private final IDataLoader dataLoader;

    /**
     * 查询API基本信息
     *
     * @param queryApiBaseInfoReq 查询API基本信息请求参数
     * @return API基本信息
     */
    @Override
    public AppServeDataResponse<List<QueryApiBaseInfoVo>> queryApiBaseInfo(QueryApiBaseInfoReq queryApiBaseInfoReq) {
        List<AppBaseApiInfo> queryResult = appBaseApiInfoService.query(queryApiBaseInfoReq);
        if(queryResult == null) {
            return AppServeResponseFactory.fail("数据服务器异常", null);
        }
        List<QueryApiBaseInfoVo> queryApiBaseInfoVos = AppBeanUtils.copyListProperties(queryResult, QueryApiBaseInfoVo::new);
        return AppServeResponseFactory.success("查询成功", queryApiBaseInfoVos);
    }

    /**
     * 重新加载API基本信息
     * @return 重新加载结果
     */
    @Override
    public AppServeResponse reloadApiKey() {
        dataLoader.loadApiKey();
        return AppServeResponseFactory.success("已经尝试重新加载");
    }

    /**
     * 添加API基本信息
     * @param addApiBaseInfoReq 添加API基本信息请求参数
     * @return 添加结果
     */
    @Override
    public AppServeResponse addApiBaseInfo(AddApiBaseInfoReq addApiBaseInfoReq) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录", null);
        }
        String userId = userIdObj.toString();
        List<AppBaseApiInfo> baseApiInfos = appBaseApiInfoService.queryWithApiKey(addApiBaseInfoReq.getApiKey());
        if(baseApiInfos == null) {
            return AppServeResponseFactory.fail("数据服务器异常");
        }
        if(!baseApiInfos.isEmpty()) {
            return AppServeResponseFactory.fail("API键已存在");
        }
        AppBaseApiInfo appBaseApiInfo = new AppBaseApiInfo();
        AppBeanUtils.copyProperties(addApiBaseInfoReq, appBaseApiInfo);
        boolean insertResult = appBaseApiInfoService.insert(appBaseApiInfo);
        if(!insertResult) {
            return AppServeResponseFactory.fail("数据服务器异常");
        }
        log.info("添加API基本信息成功，用户ID：{}，API基本信息：{}", userId, AppStringUtils.objectToString(addApiBaseInfoReq));
        return AppServeResponseFactory.success("添加成功");
    }

    /**
     * 删除API基本信息
     * @param apiKey API键
     * @return 删除结果
     */
    @Override
    public AppServeResponse deleteApiBaseInfo(String apiKey) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录", null);
        }
        String userId = userIdObj.toString();
        List<AppBaseApiInfo> baseApiInfos = appBaseApiInfoService.queryWithApiKey(apiKey);
        if(baseApiInfos == null) {
            return AppServeResponseFactory.fail("数据服务器异常");
        }
        if(baseApiInfos.isEmpty()) {
            return AppServeResponseFactory.fail("API键不存在");
        }
        boolean deleteResult = appBaseApiInfoService.delete(apiKey);
        if(!deleteResult) {
            return AppServeResponseFactory.fail("数据服务器异常");
        }
        log.info("删除API基本信息成功，用户ID：{}，API键：{}，API基础资料：{}", userId, apiKey, AppStringUtils.objectToString(baseApiInfos));
        return AppServeResponseFactory.success("删除成功");
    }

    /**
     * 更新API基本信息
     * @param updateApiBaseInfoReq 更新API基本信息请求参数
     * @return 更新结果
     */
    @Override
    public AppServeResponse updateApiBaseInfo(UpdateApiBaseInfoReq updateApiBaseInfoReq) {
        Object userIdObj = StpUtil.getLoginId();
        if(userIdObj == null) {
            return AppServeResponseFactory.unauthorized("用户未登录", null);
        }
        String userId = userIdObj.toString();
        List<AppBaseApiInfo> baseApiInfos = appBaseApiInfoService.queryWithApiKey(updateApiBaseInfoReq.getApiKey());
        if(baseApiInfos == null) {
            return AppServeResponseFactory.fail("数据服务器异常");
        }
        if(baseApiInfos.isEmpty()) {
            return AppServeResponseFactory.fail("API键不存在");
        }
        AppBaseApiInfo appBaseApiInfo = new AppBaseApiInfo();
        AppBeanUtils.copyProperties(updateApiBaseInfoReq, appBaseApiInfo);
        boolean updateResult = appBaseApiInfoService.update(appBaseApiInfo);
        if(!updateResult) {
            return AppServeResponseFactory.fail("数据服务器异常");
        }
        log.info("更新API基本信息成功，用户ID：{}，原数据：{}，新数据：{}", userId, AppStringUtils.objectToString(baseApiInfos), AppStringUtils.objectToString(updateApiBaseInfoReq));
        return AppServeResponseFactory.success("更新成功");
    }
}
