package com.seer.teach.openapi.service.impl;

import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.SignUtils;
import com.seer.teach.openapi.entity.AppKeySecretsEntity;
import com.seer.teach.openapi.service.AppKeySecretsService;
import com.seer.teach.openapi.service.SignatureService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 *
 * <p>
 *
 * </p >
 *
 * @author Captain
 * @since 2025-11-01 13:35
 */
@Service
@RequiredArgsConstructor
public class SignatureServiceImpl implements SignatureService {

    private final AppKeySecretsService appKeySecretsService;

    @Override
    public String verification(String appId, String sign, String timestamp, String body) {
        AppKeySecretsEntity appKeyEntity = appKeySecretsService.getAppKeySecretsByAppId(appId);
        AssertUtils.notNull(appKeyEntity, ResultCodeEnum.APPID_NOT_EXIST);
        String signOfServer = SignUtils.generateSign(appKeyEntity.getKey(), appKeyEntity.getSecret(), timestamp);
        if (!signOfServer.equals(sign)) {
            throw new CommonException(ResultCodeEnum.SIGN_FAIL);
        }
        return SignUtils.decrypt(body, appKeyEntity.getSecret());
    }
}
