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);
};
import KubernetesCluster from "./KubernetesCluster";
import Project from "./Project";
import User from "./User";
import BaseModel from "./DatabaseBaseModel/DatabaseBaseModel";
import Route from "../../Types/API/Route";
import ColumnAccessControl from "../../Types/Database/AccessControl/ColumnAccessControl";
import TableAccessControl from "../../Types/Database/AccessControl/TableAccessControl";
import ColumnLength from "../../Types/Database/ColumnLength";
import ColumnType from "../../Types/Database/ColumnType";
import CrudApiEndpoint from "../../Types/Database/CrudApiEndpoint";
import TableColumn from "../../Types/Database/TableColumn";
import TableColumnType from "../../Types/Database/TableColumnType";
import TableMetadata from "../../Types/Database/TableMetadata";
import TenantColumn from "../../Types/Database/TenantColumn";
import IconProp from "../../Types/Icon/IconProp";
import ObjectID from "../../Types/ObjectID";
import Permission from "../../Types/Permission";
import { Column, Entity, Index, JoinColumn, ManyToOne } from "typeorm";
/*
 * ------------------------------------------------------------------
 *                         KubernetesResource
 * ------------------------------------------------------------------
 *
 * Inventory snapshot of a single Kubernetes object (Pod, Node,
 * Deployment, ...) in a single cluster. Populated by the OTel logs
 * ingest path from the k8sobjects receiver snapshot stream
 * (kubernetes-agent pulls every 5 min by default).
 *
 * The overview page reads aggregates from this table instead of
 * groupBy-ing over 24h of ClickHouse metric/log data.
 *
 * Rows are upserted per snapshot and hard-deleted once lastSeenAt
 * falls behind "now - 15min" for clusters that remain connected.
 *
 * Writes go through KubernetesResourceService under isRoot; users
 * never create/update/delete rows directly.
 *
 * ------------------------------------------------------------------
 */
const READ_PERMISSIONS = [
    Permission.ProjectOwner,
    Permission.ProjectAdmin,
    Permission.ProjectMember,
    Permission.Viewer,
    Permission.SettingsAdmin,
    Permission.SettingsMember,
    Permission.SettingsViewer,
    Permission.ReadKubernetesCluster,
];
let KubernetesResource = class KubernetesResource extends BaseModel {
    constructor() {
        super(...arguments);
        this.project = undefined;
        this.projectId = undefined;
        this.kubernetesCluster = undefined;
        this.kubernetesClusterId = undefined;
        this.kind = undefined;
        this.namespaceKey = undefined;
        this.name = undefined;
        this.uid = undefined;
        this.phase = undefined;
        this.isReady = undefined;
        this.hasMemoryPressure = undefined;
        this.hasDiskPressure = undefined;
        this.hasPidPressure = undefined;
        this.labels = undefined;
        this.annotations = undefined;
        this.ownerReferences = undefined;
        this.spec = undefined;
        this.containerCount = undefined;
        this.status = undefined;
        this.controllerDeploymentName = undefined;
        this.controllerCronJobName = undefined;
        this.latestCpuPercent = undefined;
        this.latestMemoryBytes = undefined;
        this.latestMemoryPercent = undefined;
        this.metricsUpdatedAt = undefined;
        this.lastSeenAt = undefined;
        this.resourceCreationTimestamp = undefined;
        this.createdByUser = undefined;
        this.createdByUserId = undefined;
        this.deletedByUser = undefined;
        this.deletedByUserId = undefined;
    }
};
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        manyToOneRelationColumn: "projectId",
        type: TableColumnType.Entity,
        modelType: Project,
        title: "Project",
        description: "Relation to Project this resource belongs to.",
    }),
    ManyToOne(() => {
        return Project;
    }, {
        eager: false,
        nullable: true,
        onDelete: "CASCADE",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "projectId" }),
    __metadata("design:type", Project)
], KubernetesResource.prototype, "project", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    Index(),
    TableColumn({
        type: TableColumnType.ObjectID,
        required: true,
        canReadOnRelationQuery: true,
        title: "Project ID",
        description: "ID of the Project this resource belongs to.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: false,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], KubernetesResource.prototype, "projectId", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        manyToOneRelationColumn: "kubernetesClusterId",
        type: TableColumnType.Entity,
        modelType: KubernetesCluster,
        title: "Kubernetes Cluster",
        description: "Cluster this resource lives in.",
    }),
    ManyToOne(() => {
        return KubernetesCluster;
    }, {
        eager: false,
        nullable: true,
        onDelete: "CASCADE",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "kubernetesClusterId" }),
    __metadata("design:type", KubernetesCluster)
], KubernetesResource.prototype, "kubernetesCluster", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    Index(),
    TableColumn({
        type: TableColumnType.ObjectID,
        required: true,
        canReadOnRelationQuery: true,
        title: "Kubernetes Cluster ID",
        description: "ID of the Kubernetes Cluster this resource lives in.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: false,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], KubernetesResource.prototype, "kubernetesClusterId", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Kind",
        description: "Kubernetes resource kind in singular PascalCase (e.g. Pod, Node, Deployment).",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesResource.prototype, "kind", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Namespace Key",
        description: "Kubernetes namespace, or empty string for cluster-scoped resources. Kept non-null so the upsert unique index works.",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
        default: "",
    }),
    __metadata("design:type", String)
], KubernetesResource.prototype, "namespaceKey", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Name",
        description: "Kubernetes resource name (metadata.name).",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesResource.prototype, "name", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "UID",
        description: "Kubernetes metadata.uid — stable identity across restarts.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesResource.prototype, "uid", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Phase",
        description: "Pod.status.phase (Running / Pending / Failed / Succeeded / Unknown). Null for non-Pod kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesResource.prototype, "phase", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Boolean,
        canReadOnRelationQuery: true,
        title: "Is Ready",
        description: "True if Node.status.conditions[type=Ready].status == True. Null for non-Node kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Boolean,
    }),
    __metadata("design:type", Boolean)
], KubernetesResource.prototype, "isReady", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Boolean,
        canReadOnRelationQuery: true,
        title: "Has Memory Pressure",
        description: "Node MemoryPressure condition. Null for non-Node kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Boolean,
    }),
    __metadata("design:type", Boolean)
], KubernetesResource.prototype, "hasMemoryPressure", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Boolean,
        canReadOnRelationQuery: true,
        title: "Has Disk Pressure",
        description: "Node DiskPressure condition. Null for non-Node kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Boolean,
    }),
    __metadata("design:type", Boolean)
], KubernetesResource.prototype, "hasDiskPressure", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Boolean,
        canReadOnRelationQuery: true,
        title: "Has PID Pressure",
        description: "Node PIDPressure condition. Null for non-Node kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Boolean,
    }),
    __metadata("design:type", Boolean)
], KubernetesResource.prototype, "hasPidPressure", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.JSON,
        title: "Labels",
        description: "Kubernetes metadata.labels.",
    }),
    Column({
        nullable: true,
        type: ColumnType.JSON,
    }),
    __metadata("design:type", Object)
], KubernetesResource.prototype, "labels", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.JSON,
        title: "Annotations",
        description: "Kubernetes metadata.annotations.",
    }),
    Column({
        nullable: true,
        type: ColumnType.JSON,
    }),
    __metadata("design:type", Object)
], KubernetesResource.prototype, "annotations", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.JSON,
        title: "Owner References",
        description: "Kubernetes metadata.ownerReferences array.",
    }),
    Column({
        nullable: true,
        type: ColumnType.JSON,
    }),
    __metadata("design:type", Object)
], KubernetesResource.prototype, "ownerReferences", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.JSON,
        title: "Spec",
        description: "Kubernetes spec block, verbatim from the last snapshot.",
    }),
    Column({
        nullable: true,
        type: ColumnType.JSON,
    }),
    __metadata("design:type", Object)
], KubernetesResource.prototype, "spec", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Number,
        canReadOnRelationQuery: true,
        title: "Container Count",
        description: "For Pods: count of entries in spec.containers, cached so the overview page can SUM it without scanning JSONB. Null for non-Pod kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Number,
    }),
    __metadata("design:type", Number)
], KubernetesResource.prototype, "containerCount", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.JSON,
        title: "Status",
        description: "Kubernetes status block, verbatim from the last snapshot.",
    }),
    Column({
        nullable: true,
        type: ColumnType.JSON,
    }),
    __metadata("design:type", Object)
], KubernetesResource.prototype, "status", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Controller Deployment Name",
        description: "For Pod kinds: the Deployment that ultimately owns this Pod (one hop above the ReplicaSet). Populated from `resource.k8s.deployment.name` on the metric stream so the Deployments list view can SUM by this column without inventorying ReplicaSets. Null for non-Pod kinds and for Pods not owned by a Deployment.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesResource.prototype, "controllerDeploymentName", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Controller CronJob Name",
        description: "For Pod kinds: the CronJob that ultimately owns this Pod (one hop above the Job). Populated from `resource.k8s.cronjob.name` on the metric stream. Null for non-Pod kinds and Pods not owned by a CronJob.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesResource.prototype, "controllerCronJobName", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Number,
        canReadOnRelationQuery: true,
        title: "Latest CPU Percent",
        description: "Most recent CPU utilization percent for this resource (Pod or Node). Stored as decimal so sub-percent precision survives the round trip. Can exceed 100 when the resource consumes more than one core. Null until the first metric arrives.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Decimal,
        transformer: {
            to: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                return value;
            },
            from: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                if (typeof value === "number") {
                    return value;
                }
                const parsed = parseFloat(value);
                return isNaN(parsed) ? null : parsed;
            },
        },
    }),
    __metadata("design:type", Number)
], KubernetesResource.prototype, "latestCpuPercent", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.BigPositiveNumber,
        canReadOnRelationQuery: true,
        title: "Latest Memory Bytes",
        description: "Most recent memory usage (bytes) for this resource (Pod or Node). Stored as bigint so values past 2 GiB don't overflow. Same lifecycle as latestCpuPercent.",
    }),
    Column({
        nullable: true,
        type: ColumnType.BigPositiveNumber,
        transformer: {
            to: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                return Math.trunc(value).toString();
            },
            from: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                const parsed = parseInt(value, 10);
                return isNaN(parsed) ? null : parsed;
            },
        },
    }),
    __metadata("design:type", Number)
], KubernetesResource.prototype, "latestMemoryBytes", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Number,
        canReadOnRelationQuery: true,
        title: "Latest Memory Percent",
        description: "Most recent memory usage as a percent of the resource's node allocatable memory (Pod or Node). Stored as decimal — mirrors latestCpuPercent — so the workload/namespace list views can SUM a per-pod percentage. Null until the first metric arrives or while the node's allocatable memory is still unknown.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Decimal,
        transformer: {
            to: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                return value;
            },
            from: (value) => {
                if (value === null || value === undefined) {
                    return null;
                }
                if (typeof value === "number") {
                    return value;
                }
                const parsed = parseFloat(value);
                return isNaN(parsed) ? null : parsed;
            },
        },
    }),
    __metadata("design:type", Number)
], KubernetesResource.prototype, "latestMemoryPercent", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Date,
        canReadOnRelationQuery: true,
        title: "Metrics Updated At",
        description: "Observed timestamp of the latest CPU/memory point. Acts as the monotonic guard for metric updates and the cutoff for staleness rendering.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Date,
    }),
    __metadata("design:type", Date)
], KubernetesResource.prototype, "metricsUpdatedAt", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.Date,
        canReadOnRelationQuery: true,
        title: "Last Seen At",
        description: "Agent-observed timestamp of the most recent snapshot for this resource. Also acts as the monotonic guard for upserts.",
    }),
    Column({
        nullable: false,
        type: ColumnType.Date,
    }),
    __metadata("design:type", Date)
], KubernetesResource.prototype, "lastSeenAt", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Date,
        canReadOnRelationQuery: true,
        title: "Resource Creation Timestamp",
        description: "Kubernetes metadata.creationTimestamp of this object.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Date,
    }),
    __metadata("design:type", Date)
], KubernetesResource.prototype, "resourceCreationTimestamp", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        manyToOneRelationColumn: "createdByUserId",
        type: TableColumnType.Entity,
        modelType: User,
        title: "Created By User",
        description: "Not user-facing; ingest writes as isRoot so this stays null in practice.",
    }),
    ManyToOne(() => {
        return User;
    }, {
        eager: false,
        nullable: true,
        onDelete: "SET NULL",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "createdByUserId" }),
    __metadata("design:type", User)
], KubernetesResource.prototype, "createdByUser", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        type: TableColumnType.ObjectID,
        title: "Created By User ID",
        description: "ID of the user who created this row.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: true,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], KubernetesResource.prototype, "createdByUserId", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        manyToOneRelationColumn: "deletedByUserId",
        type: TableColumnType.Entity,
        modelType: User,
        title: "Deleted By User",
        description: "Relation to the user who deleted this row.",
    }),
    ManyToOne(() => {
        return User;
    }, {
        eager: false,
        nullable: true,
        onDelete: "SET NULL",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "deletedByUserId" }),
    __metadata("design:type", User)
], KubernetesResource.prototype, "deletedByUser", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        type: TableColumnType.ObjectID,
        title: "Deleted By User ID",
        description: "ID of the user who deleted this row.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: true,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], KubernetesResource.prototype, "deletedByUserId", void 0);
KubernetesResource = __decorate([
    TenantColumn("projectId"),
    TableAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
        delete: [],
    }),
    CrudApiEndpoint(new Route("/kubernetes-resource")),
    TableMetadata({
        tableName: "KubernetesResource",
        singularName: "Kubernetes Resource",
        pluralName: "Kubernetes Resources",
        icon: IconProp.Cube,
        tableDescription: "Snapshot of a Kubernetes object (pod, node, deployment, etc.) as last reported by the kubernetes-agent. Populated by the telemetry ingest pipeline; not user-editable.",
    }),
    Index(["projectId", "kubernetesClusterId", "kind", "namespaceKey", "name"], {
        unique: true,
    }),
    Entity({
        name: "KubernetesResource",
    })
], KubernetesResource);
export default KubernetesResource;
//# sourceMappingURL=KubernetesResource.js.map