import AnalyticsBaseModel from "./AnalyticsBaseModel/AnalyticsBaseModel";
import Route from "../../Types/API/Route";
import AnalyticsTableEngine from "../../Types/AnalyticsDatabase/AnalyticsTableEngine";
import AnalyticsTableName from "../../Types/AnalyticsDatabase/AnalyticsTableName";
import AnalyticsTableColumn from "../../Types/AnalyticsDatabase/TableColumn";
import TableColumnType from "../../Types/AnalyticsDatabase/TableColumnType";
import Permission from "../../Types/Permission";
import { PlanType } from "../../Types/Billing/SubscriptionPlan";
export default class AuditLog extends AnalyticsBaseModel {
    constructor() {
        const projectIdColumn = new AnalyticsTableColumn({
            key: "projectId",
            title: "Project ID",
            description: "ID of the project this audit log belongs to.",
            required: true,
            type: TableColumnType.ObjectID,
            isTenantId: true,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const resourceTypeColumn = new AnalyticsTableColumn({
            key: "resourceType",
            title: "Resource Type",
            description: "Type of the resource that was changed (e.g. Incident, Monitor).",
            required: true,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const resourceIdColumn = new AnalyticsTableColumn({
            key: "resourceId",
            title: "Resource ID",
            description: "ID of the resource that was changed.",
            required: true,
            type: TableColumnType.ObjectID,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const resourceNameColumn = new AnalyticsTableColumn({
            key: "resourceName",
            title: "Resource Name",
            description: "Display name of the resource at the time of the change.",
            required: false,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const actionColumn = new AnalyticsTableColumn({
            key: "action",
            title: "Action",
            description: "The action performed: Create, Update, or Delete.",
            required: true,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const userIdColumn = new AnalyticsTableColumn({
            key: "userId",
            title: "User ID",
            description: "ID of the user who performed the action, if any.",
            required: false,
            type: TableColumnType.ObjectID,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const userNameColumn = new AnalyticsTableColumn({
            key: "userName",
            title: "User Name",
            description: "Display name of the user at the time of the action.",
            required: false,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const userEmailColumn = new AnalyticsTableColumn({
            key: "userEmail",
            title: "User Email",
            description: "Email of the user at the time of the action.",
            required: false,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const userTypeColumn = new AnalyticsTableColumn({
            key: "userType",
            title: "User Type",
            description: "Type of actor: User, API, Automation, MasterAdmin, or System.",
            required: false,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const apiKeyIdColumn = new AnalyticsTableColumn({
            key: "apiKeyId",
            title: "API Key ID",
            description: "ID of the API key used for this action, if performed via API.",
            required: false,
            type: TableColumnType.ObjectID,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const apiKeyNameColumn = new AnalyticsTableColumn({
            key: "apiKeyName",
            title: "API Key Name",
            description: "Display name of the API key at the time of the action.",
            required: false,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const changesColumn = new AnalyticsTableColumn({
            key: "changes",
            title: "Changes",
            description: "Field-level changes for Update actions, or full snapshot for Create/Delete. Redacted fields are omitted.",
            required: true,
            defaultValue: [],
            type: TableColumnType.JSONArray,
            codec: { codec: "ZSTD", level: 3 },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
            },
        });
        const retentionDateColumn = new AnalyticsTableColumn({
            key: "retentionDate",
            title: "Retention Date",
            description: "Date after which this row is eligible for TTL deletion, computed at ingest time as createdAt + project.auditLogsRetentionInDays.",
            required: true,
            type: TableColumnType.Date,
            defaultValue: undefined,
        });
        super({
            tableName: AnalyticsTableName.AuditLog,
            tableEngine: AnalyticsTableEngine.MergeTree,
            singularName: "Audit Log",
            pluralName: "Audit Logs",
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.SettingsAdmin,
                    Permission.ReadAuditLog,
                ],
                create: [Permission.ProjectOwner, Permission.ProjectAdmin],
                update: [],
                delete: [],
            },
            tableBillingAccessControl: {
                create: PlanType.Enterprise,
                read: PlanType.Enterprise,
                update: PlanType.Enterprise,
                delete: PlanType.Enterprise,
            },
            crudApiPath: new Route("/audit-log"),
            enableDocumentation: true,
            tableDescription: "Immutable audit-trail records of actions taken within your project.",
            tableColumns: [
                projectIdColumn,
                resourceTypeColumn,
                resourceIdColumn,
                resourceNameColumn,
                actionColumn,
                userIdColumn,
                userNameColumn,
                userEmailColumn,
                userTypeColumn,
                apiKeyIdColumn,
                apiKeyNameColumn,
                changesColumn,
                retentionDateColumn,
            ],
            projections: [],
            sortKeys: ["projectId", "createdAt", "resourceType", "resourceId"],
            primaryKeys: ["projectId", "createdAt"],
            partitionKey: "toYYYYMM(createdAt)",
            tableSettings: "ttl_only_drop_parts = 1, non_replicated_deduplication_window = 10000",
            ttlExpression: "retentionDate DELETE",
            /*
             * `createdAt` already participates in the AuditLog sort key
             * (position 2 after `projectId`), so the legacy `createdAt
             * DESC` default was already efficient here. Set it explicitly
             * so the choice is intentional rather than inherited from the
             * base class.
             */
            defaultSortColumn: "createdAt",
        });
    }
    get projectId() {
        return this.getColumnValue("projectId");
    }
    set projectId(v) {
        this.setColumnValue("projectId", v);
    }
    get resourceType() {
        return this.getColumnValue("resourceType");
    }
    set resourceType(v) {
        this.setColumnValue("resourceType", v);
    }
    get resourceId() {
        return this.getColumnValue("resourceId");
    }
    set resourceId(v) {
        this.setColumnValue("resourceId", v);
    }
    get resourceName() {
        return this.getColumnValue("resourceName");
    }
    set resourceName(v) {
        this.setColumnValue("resourceName", v);
    }
    get action() {
        return this.getColumnValue("action");
    }
    set action(v) {
        this.setColumnValue("action", v);
    }
    get userId() {
        return this.getColumnValue("userId");
    }
    set userId(v) {
        this.setColumnValue("userId", v);
    }
    get userName() {
        return this.getColumnValue("userName");
    }
    set userName(v) {
        this.setColumnValue("userName", v);
    }
    get userEmail() {
        return this.getColumnValue("userEmail");
    }
    set userEmail(v) {
        this.setColumnValue("userEmail", v);
    }
    get userType() {
        return this.getColumnValue("userType");
    }
    set userType(v) {
        this.setColumnValue("userType", v);
    }
    get apiKeyId() {
        return this.getColumnValue("apiKeyId");
    }
    set apiKeyId(v) {
        this.setColumnValue("apiKeyId", v);
    }
    get apiKeyName() {
        return this.getColumnValue("apiKeyName");
    }
    set apiKeyName(v) {
        this.setColumnValue("apiKeyName", v);
    }
    get changes() {
        return this.getColumnValue("changes");
    }
    set changes(v) {
        this.setColumnValue("changes", v);
    }
    get retentionDate() {
        return this.getColumnValue("retentionDate");
    }
    set retentionDate(v) {
        this.setColumnValue("retentionDate", v);
    }
}
//# sourceMappingURL=AuditLog.js.map