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

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

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(UserKeyDto userKeyDto) {
        String domain = userKeyDto.getDomain();
        int userId = UserContext.getUser().getId();
        int total = userKeyRepository.countByDomainAndUserId(domain, userId);
        if (total > 2) {
            String errMsg = String.format("you already have %s keys, key exceed limit", total);
            return Result.fail(errMsg);
        }

        String accessKeyId = RandomString.getString(8);
        String accessKeySecret = RandomString.getString(18);
        UserKey userKey = new UserKey(accessKeyId, accessKeySecret, domain, userId);
        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");
        }

        long loginUser = userKey.getUserId();
        String token = getToken(loginUser);

        return Result.success(token);
    }

    public String getToken(long loginUser) {
        String secretKey = "ossconsole.reghao.cn";
        long timestamp = System.currentTimeMillis() + 3600*24*365;
        String action = ChannelAction.download.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().intValue();
    }

    public void delete(String accessKeyId) {
    }

    public List<UserKey> getUserKeys() {
        int userId = UserContext.getUser().getId();
        List<UserKey> list = userKeyRepository.findByUserId(userId);
        return list;
    }
}
