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";
/*
 * ------------------------------------------------------------------
 *                         KubernetesContainer
 * ------------------------------------------------------------------
 *
 * One row per container in the Pod inventory snapshot. Lets the
 * Containers list page do a single indexed SELECT instead of
 * fetching every Pod and unpacking JSONB at render time, and lets
 * the metric ingest path write latest CPU/memory directly so the
 * page never needs to hit ClickHouse.
 *
 * Populated together with KubernetesResource: when a Pod snapshot
 * arrives, the ingest path emits one parent KubernetesResource row
 * (kind = "Pod") and N child KubernetesContainer rows.
 *
 * Hard-deleted alongside its parent Pod by the cleanup worker once
 * the Pod's lastSeenAt falls behind the staleness threshold.
 *
 * ------------------------------------------------------------------
 */
const READ_PERMISSIONS = [
    Permission.ProjectOwner,
    Permission.ProjectAdmin,
    Permission.ProjectMember,
    Permission.Viewer,
    Permission.SettingsAdmin,
    Permission.SettingsMember,
    Permission.SettingsViewer,
    Permission.ReadKubernetesCluster,
];
let KubernetesContainer = class KubernetesContainer extends BaseModel {
    constructor() {
        super(...arguments);
        this.project = undefined;
        this.projectId = undefined;
        this.kubernetesCluster = undefined;
        this.kubernetesClusterId = undefined;
        this.podNamespaceKey = undefined;
        this.podName = undefined;
        this.name = undefined;
        this.image = undefined;
        this.state = undefined;
        this.reason = undefined;
        this.isReady = undefined;
        this.restartCount = undefined;
        this.memoryLimitBytes = undefined;
        this.latestCpuPercent = undefined;
        this.latestMemoryBytes = undefined;
        this.metricsUpdatedAt = undefined;
        this.lastSeenAt = 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 container belongs to.",
    }),
    ManyToOne(() => {
        return Project;
    }, {
        eager: false,
        nullable: true,
        onDelete: "CASCADE",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "projectId" }),
    __metadata("design:type", Project)
], KubernetesContainer.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 container belongs to.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: false,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], KubernetesContainer.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 container lives in.",
    }),
    ManyToOne(() => {
        return KubernetesCluster;
    }, {
        eager: false,
        nullable: true,
        onDelete: "CASCADE",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "kubernetesClusterId" }),
    __metadata("design:type", KubernetesCluster)
], KubernetesContainer.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 container lives in.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: false,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], KubernetesContainer.prototype, "kubernetesClusterId", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Pod Namespace Key",
        description: "Namespace of the parent Pod. Empty string for the rare cluster-scoped pod case; kept non-null so the upsert unique index works.",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
        default: "",
    }),
    __metadata("design:type", String)
], KubernetesContainer.prototype, "podNamespaceKey", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Pod Name",
        description: "metadata.name of the parent Pod.",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesContainer.prototype, "podName", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Name",
        description: "Container name (spec.containers[].name).",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesContainer.prototype, "name", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.LongText,
        canReadOnRelationQuery: true,
        title: "Image",
        description: "Container image reference (spec.containers[].image).",
    }),
    Column({
        nullable: true,
        type: ColumnType.LongText,
        length: ColumnLength.LongText,
    }),
    __metadata("design:type", String)
], KubernetesContainer.prototype, "image", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "State",
        description: "Container state from status.containerStatuses[].state (running / waiting / terminated). Null until status is observed.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesContainer.prototype, "state", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Reason",
        description: "Container state reason (e.g. ImagePullBackOff, CrashLoopBackOff). Surfaced on the list page when the container is not Running.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], KubernetesContainer.prototype, "reason", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Boolean,
        canReadOnRelationQuery: true,
        title: "Is Ready",
        description: "True when status.containerStatuses[].ready is true. Null until status is observed.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Boolean,
    }),
    __metadata("design:type", Boolean)
], KubernetesContainer.prototype, "isReady", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.PositiveNumber,
        canReadOnRelationQuery: true,
        title: "Restart Count",
        description: "Restart counter from status.containerStatuses[].restartCount.",
    }),
    Column({
        nullable: true,
        type: ColumnType.PositiveNumber,
    }),
    __metadata("design:type", Number)
], KubernetesContainer.prototype, "restartCount", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.BigPositiveNumber,
        canReadOnRelationQuery: true,
        title: "Memory Limit Bytes",
        description: "spec.containers[].resources.limits.memory parsed to bytes, used to render the % memory bar on the list view.",
    }),
    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)
], KubernetesContainer.prototype, "memoryLimitBytes", 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 container. Stored as decimal so sub-percent precision survives the round trip. 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)
], KubernetesContainer.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 container. 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)
], KubernetesContainer.prototype, "latestMemoryBytes", 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)
], KubernetesContainer.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 Pod snapshot containing this container. Acts as the monotonic guard for upserts and the cutoff for stale-row cleanup.",
    }),
    Column({
        nullable: false,
        type: ColumnType.Date,
    }),
    __metadata("design:type", Date)
], KubernetesContainer.prototype, "lastSeenAt", 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)
], KubernetesContainer.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)
], KubernetesContainer.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)
], KubernetesContainer.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)
], KubernetesContainer.prototype, "deletedByUserId", void 0);
KubernetesContainer = __decorate([
    TenantColumn("projectId"),
    TableAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
        delete: [],
    }),
    CrudApiEndpoint(new Route("/kubernetes-container")),
    TableMetadata({
        tableName: "KubernetesContainer",
        singularName: "Kubernetes Container",
        pluralName: "Kubernetes Containers",
        icon: IconProp.Cube,
        tableDescription: "Snapshot of a single container inside a Pod, populated by the telemetry ingest pipeline. Not user-editable.",
    }),
    Index(["projectId", "kubernetesClusterId", "podNamespaceKey", "podName", "name"], { unique: true }),
    Entity({
        name: "KubernetesContainer",
    })
], KubernetesContainer);
export default KubernetesContainer;
//# sourceMappingURL=KubernetesContainer.js.map