"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordEnterpriseAuditEvent = recordEnterpriseAuditEvent;
exports.recordEnterpriseAuditEventForContext = recordEnterpriseAuditEventForContext;
exports.listEnterpriseAuditEvents = listEnterpriseAuditEvents;
const crypto_1 = __importDefault(require("crypto"));
const config_1 = require("../config");
const enterpriseDb_1 = require("./enterpriseDb");
function ensureAuditGraph(db, input) {
    const now = Date.now();
    db.prepare(`
    INSERT OR IGNORE INTO organizations (id, name, status, plan, created_at, updated_at)
    VALUES (?, ?, 'active', 'enterprise', ?, ?)
  `).run(input.tenantId, input.tenantId, now, now);
    if (input.workspaceId) {
        db.prepare(`
      INSERT OR IGNORE INTO workspaces (id, tenant_id, name, created_at, updated_at)
      VALUES (?, ?, ?, ?, ?)
    `).run(input.workspaceId, input.tenantId, input.workspaceId, now, now);
    }
    if (input.actorUserId) {
        db.prepare(`
      INSERT OR IGNORE INTO users (id, tenant_id, email, display_name, idp_subject, created_at, updated_at)
      VALUES (?, ?, ?, ?, ?, ?, ?)
    `).run(input.actorUserId, input.tenantId, `${input.actorUserId}@audit.local`, input.actorUserId, `audit:${input.tenantId}:${input.actorUserId}`, now, now);
    }
}
function recordEnterpriseAuditEvent(db, input) {
    ensureAuditGraph(db, input);
    db.prepare(`
    INSERT INTO audit_events
      (id, tenant_id, workspace_id, actor_user_id, action, resource_type, resource_id, metadata_json, created_at)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
  `).run(crypto_1.default.randomUUID(), input.tenantId, input.workspaceId ?? null, input.actorUserId ?? null, input.action, input.resourceType, input.resourceId ?? null, input.metadata ? JSON.stringify(input.metadata) : null, Date.now());
}
function recordEnterpriseAuditEventForContext(context, input) {
    if (!(0, config_1.resolveFeatureConfig)().enterprise)
        return false;
    const db = (0, enterpriseDb_1.openEnterpriseDb)();
    try {
        recordEnterpriseAuditEvent(db, {
            tenantId: context.tenantId,
            workspaceId: input.workspaceId === undefined ? context.workspaceId : input.workspaceId ?? undefined,
            actorUserId: context.userId,
            action: input.action,
            resourceType: input.resourceType,
            resourceId: input.resourceId,
            metadata: input.metadata,
        });
        return true;
    }
    catch (error) {
        console.warn('[EnterpriseAudit] Failed to record audit event:', error.message);
        return false;
    }
    finally {
        db.close();
    }
}
function listEnterpriseAuditEvents(db) {
    return db.prepare(`
    SELECT action, resource_type, resource_id, tenant_id, workspace_id, actor_user_id
    FROM audit_events
    ORDER BY created_at ASC
  `).all();
}
//# sourceMappingURL=enterpriseAuditService.js.map