package cn.reghao.oss.console.app.service;

import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.security.RandomString;
import cn.reghao.oss.console.app.model.vo.UserKeyVo;
import cn.reghao.oss.storeapi.constant.ChannelAction;
import cn.reghao.oss.storeapi.dto.OssPayload;
import cn.reghao.oss.console.account.service.UserContext;
import cn.reghao.oss.console.app.db.repository.UserKeyRepository;
import cn.reghao.oss.console.app.model.dto.KeyAuthDto;
import cn.reghao.oss.console.app.model.po.UserKey;
import cn.reghao.oss.console.util.JwtUtil;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author reghao
 * @date 2024-02-27 10:25:25
 */
@Service
public class UserKeyService {
    private final UserKeyRepository userKeyRepository;

    public UserKeyService(UserKeyRepository userKeyRepository) {
        this.userKeyRepository = userKeyRepository;
    }

    public Result create() {
        int loginUser = UserContext.getUser().getId();
        UserKey userKey = userKeyRepository.findByCreateBy(loginUser);
        if (userKey != null) {
            String errMsg = String.format("you already have key");
            return Result.fail(errMsg);
        } else {
            String accessKeyId = RandomString.getString(8);
            String accessKeySecret = RandomString.getString(18);
            userKey = new UserKey(accessKeyId, accessKeySecret, loginUser);
            userKeyRepository.save(userKey);
        }

        return Result.success("key has created");
    }

    public Result auth(KeyAuthDto keyAuthDto) {
        String accessKeyId = keyAuthDto.getAccessKeyId();
        UserKey userKey = userKeyRepository.findByAccessKeyId(accessKeyId);
        if (userKey == null) {
            return Result.fail("key not exist");
        }

        if (!userKey.getAccessKeySecret().equals(keyAuthDto.getAccessKeySecret())) {
            return Result.fail("secret not matched");
        }

        int loginUser = userKey.getCreateBy();
        String token = getToken(loginUser);
        return Result.success(token);
    }

    public String getToken(int loginUser) {
        String secretKey = "ossconsole.reghao.cn";
        long timestamp = System.currentTimeMillis() + 3600*24*365*1000L;
        String action = ChannelAction.all.getName();
        int channelId = 1;
        OssPayload ossPayload = new OssPayload(action, channelId, loginUser);
        return JwtUtil.createToken(ossPayload, timestamp, secretKey);
    }

    public int getUserIdFromToken(String token) {
        String secretKey = "ossconsole.reghao.cn";
        OssPayload ossPayload = JwtUtil.getOssPayload(token, secretKey);
        return ossPayload.getUserId();
    }

    public void delete(String accessKeyId) {
        UserKey userKey = userKeyRepository.findByAccessKeyId(accessKeyId);
        if (userKey != null) {
            userKeyRepository.delete(userKey);
        }
    }

    public List<UserKeyVo> getUserKeys() {
        int loginUser = UserContext.getUser().getId();
        UserKey userKey = userKeyRepository.findByCreateBy(loginUser);
        if (userKey == null) {
            return Collections.emptyList();
        }

        String accessKeyId = userKey.getAccessKeyId();
        String accessKeySecret = userKey.getAccessKeySecret();
        return List.of(new UserKeyVo(accessKeyId, accessKeySecret));
    }
}
