"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialProviderService = void 0;
const common_1 = require("@nestjs/common");
const credentials_entity_1 = require("../../management/entities/credentials.entity");
const provider_credentials_repository_1 = require("../../access/repositories/provider-credentials.repository");
const api_token_repository_1 = require("../../access/repositories/api-token.repository");
const key_storage_service_1 = require("../../access/services/key-storage.service");
const credential_purpose_enum_1 = require("../../access/enums/credential-purpose.enum");
let CredentialProviderService = class CredentialProviderService {
    constructor(providerCredentialsRepository, apiTokenRepository, keyStorage) {
        this.providerCredentialsRepository = providerCredentialsRepository;
        this.apiTokenRepository = apiTokenRepository;
        this.keyStorage = keyStorage;
    }
    async getActiveApiToken(provider, purpose = credential_purpose_enum_1.CredentialPurpose.COMPUTE) {
        const tokens = await this.apiTokenRepository.findByProviderAndPurpose(provider, purpose);
        if (!tokens || tokens.length === 0) {
            throw new common_1.NotFoundException('No active API token found');
        }
        const token = tokens[0];
        return await this.getDecryptApiToken(token.id);
    }
    async getActiveAccessKeyPair(provider, purpose = credential_purpose_enum_1.CredentialPurpose.COMPUTE) {
        const tokens = await this.apiTokenRepository.findByProviderAndPurpose(provider, purpose);
        const token = tokens?.find((t) => t.credential_type === credentials_entity_1.CredentialType.ACCESS_KEY_SECRET);
        if (!token) {
            throw new common_1.NotFoundException(`No active access key pair found for provider ${provider}`);
        }
        const secretKey = this.keyStorage.decryptKeyFromString(token.encrypted_token);
        const accessKey = token.encrypted_access_key
            ? this.keyStorage.decryptKeyFromString(token.encrypted_access_key)
            : '';
        return { accessKey, secretKey };
    }
    async getActiveBearerToken(provider) {
        const providerCredentials = await this.providerCredentialsRepository.findByProvider(provider);
        if (!providerCredentials || providerCredentials.length === 0) {
            throw new common_1.NotFoundException('No active credentials found');
        }
        const credentials = providerCredentials[0];
        // Return the bearer token data
        return {
            access_token: credentials.access_token,
            refresh_token: credentials.refresh_token,
            expires_in: credentials.token_expires_at
                ? Math.floor((credentials.token_expires_at.getTime() - Date.now()) / 1000)
                : 3600,
            token_type: 'Bearer',
            scope: '',
        };
    }
    async getDecryptApiToken(id) {
        try {
            const encryptedToken = await this.apiTokenRepository.getEncryptedToken(id);
            return this.keyStorage.decryptKeyFromString(encryptedToken);
        }
        catch {
            throw new common_1.NotFoundException(`API token with ID ${id} not found`);
        }
    }
};
exports.CredentialProviderService = CredentialProviderService;
exports.CredentialProviderService = CredentialProviderService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [provider_credentials_repository_1.ProviderCredentialsRepository,
        api_token_repository_1.ApiTokenRepository,
        key_storage_service_1.KeyStorageService])
], CredentialProviderService);
