"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.McpApiKeyService = void 0;
const api_pagination_1 = require("@hed-hog/api-pagination");
const api_prisma_1 = require("@hed-hog/api-prisma");
const common_1 = require("@nestjs/common");
const security_service_1 = require("../security/security.service");
let McpApiKeyService = class McpApiKeyService {
    constructor(prisma, security, pagination) {
        this.prisma = prisma;
        this.security = security;
        this.pagination = pagination;
    }
    async list(userId, paginationParams) {
        return this.pagination.paginatePrismaModel(this.prisma.mcp_api_key, Object.assign(Object.assign({}, paginationParams), { where: { user_id: userId, revoked_at: null }, select: {
                id: true,
                name: true,
                token_prefix: true,
                last_used_at: true,
                expires_at: true,
                created_at: true,
            }, orderBy: { created_at: 'desc' } }));
    }
    async create(userId, name) {
        const rawToken = 'hedhog_mcp_' + this.security.randomOpaque(36);
        const tokenHash = this.security.hashWithPepper(rawToken);
        const tokenPrefix = rawToken.substring(0, 16);
        const record = await this.prisma.mcp_api_key.create({
            data: {
                user_id: userId,
                name,
                token_hash: tokenHash,
                token_prefix: tokenPrefix,
            },
            select: {
                id: true,
                name: true,
                token_prefix: true,
                created_at: true,
            },
        });
        return Object.assign(Object.assign({}, record), { rawToken });
    }
    async revoke(userId, id) {
        const key = await this.prisma.mcp_api_key.findFirst({
            where: { id, user_id: userId },
        });
        if (!key) {
            throw new common_1.NotFoundException(`MCP API key ${id} not found`);
        }
        await this.prisma.mcp_api_key.update({
            where: { id },
            data: { revoked_at: new Date() },
        });
        return { success: true };
    }
};
exports.McpApiKeyService = McpApiKeyService;
exports.McpApiKeyService = McpApiKeyService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [api_prisma_1.PrismaService,
        security_service_1.SecurityService,
        api_pagination_1.PaginationService])
], McpApiKeyService);
//# sourceMappingURL=mcp-api-key.service.js.map