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 DockerHost from "./DockerHost";
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";
/*
 * ------------------------------------------------------------------
 *                          DockerResource
 * ------------------------------------------------------------------
 *
 * Inventory snapshot of a single Docker object (Container, Image,
 * Network, Volume) on a single Docker host. Populated by the
 * telemetry ingest pipeline; not user-editable.
 *
 * Container kind is populated from the docker_stats receiver metric
 * stream (every metric carries container.id / container.name /
 * container.image.name resource attributes). Image / Network /
 * Volume kinds are reserved for a follow-up agent change that adds a
 * snapshot poller.
 *
 * Rows are upserted on every metric flush and hard-deleted once
 * lastSeenAt falls behind a staleness threshold.
 *
 * ------------------------------------------------------------------
 */
const READ_PERMISSIONS = [
    Permission.ProjectOwner,
    Permission.ProjectAdmin,
    Permission.ProjectMember,
    Permission.Viewer,
    Permission.SettingsAdmin,
    Permission.SettingsMember,
    Permission.SettingsViewer,
    Permission.ReadDockerHost,
];
let DockerResource = class DockerResource extends BaseModel {
    constructor() {
        super(...arguments);
        this.project = undefined;
        this.projectId = undefined;
        this.dockerHost = undefined;
        this.dockerHostId = undefined;
        this.kind = undefined;
        this.name = undefined;
        this.containerId = undefined;
        this.imageName = undefined;
        this.state = undefined;
        this.labels = undefined;
        this.latestCpuPercent = undefined;
        this.latestMemoryBytes = 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)
], DockerResource.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)
], DockerResource.prototype, "projectId", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        manyToOneRelationColumn: "dockerHostId",
        type: TableColumnType.Entity,
        modelType: DockerHost,
        title: "Docker Host",
        description: "Host this resource lives on.",
    }),
    ManyToOne(() => {
        return DockerHost;
    }, {
        eager: false,
        nullable: true,
        onDelete: "CASCADE",
        orphanedRowAction: "nullify",
    }),
    JoinColumn({ name: "dockerHostId" }),
    __metadata("design:type", DockerHost)
], DockerResource.prototype, "dockerHost", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    Index(),
    TableColumn({
        type: TableColumnType.ObjectID,
        required: true,
        canReadOnRelationQuery: true,
        title: "Docker Host ID",
        description: "ID of the Docker Host this resource lives on.",
    }),
    Column({
        type: ColumnType.ObjectID,
        nullable: false,
        transformer: ObjectID.getDatabaseTransformer(),
    }),
    __metadata("design:type", ObjectID)
], DockerResource.prototype, "dockerHostId", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Kind",
        description: "Docker resource kind in singular PascalCase (Container, Image, Network, Volume).",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], DockerResource.prototype, "kind", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: true,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Name",
        description: "Docker resource name. Container.name for containers, full image reference for images.",
    }),
    Column({
        nullable: false,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], DockerResource.prototype, "name", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Container ID",
        description: "Docker container short id (12 chars). Null for non-Container kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], DockerResource.prototype, "containerId", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "Image Name",
        description: "Image reference for the container (e.g. nginx:1.27). Null for non-Container kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], DockerResource.prototype, "imageName", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.ShortText,
        canReadOnRelationQuery: true,
        title: "State",
        description: "Container state (running, exited, paused, restarting, dead, created). Null for non-Container kinds.",
    }),
    Column({
        nullable: true,
        type: ColumnType.ShortText,
        length: ColumnLength.ShortText,
    }),
    __metadata("design:type", String)
], DockerResource.prototype, "state", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.JSON,
        title: "Labels",
        description: "Docker labels attached to this resource.",
    }),
    Column({
        nullable: true,
        type: ColumnType.JSON,
    }),
    __metadata("design:type", Object)
], DockerResource.prototype, "labels", 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 or for non-Container kinds.",
    }),
    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)
], DockerResource.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. Stored as bigint so values past 2 GiB don't overflow.",
    }),
    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)
], DockerResource.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.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Date,
    }),
    __metadata("design:type", Date)
], DockerResource.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)
], DockerResource.prototype, "lastSeenAt", void 0);
__decorate([
    ColumnAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
    }),
    TableColumn({
        required: false,
        type: TableColumnType.Date,
        canReadOnRelationQuery: true,
        title: "Resource Creation Timestamp",
        description: "When this resource was created in Docker.",
    }),
    Column({
        nullable: true,
        type: ColumnType.Date,
    }),
    __metadata("design:type", Date)
], DockerResource.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)
], DockerResource.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)
], DockerResource.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)
], DockerResource.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)
], DockerResource.prototype, "deletedByUserId", void 0);
DockerResource = __decorate([
    TenantColumn("projectId"),
    TableAccessControl({
        create: [],
        read: READ_PERMISSIONS,
        update: [],
        delete: [],
    }),
    CrudApiEndpoint(new Route("/docker-resource")),
    TableMetadata({
        tableName: "DockerResource",
        singularName: "Docker Resource",
        pluralName: "Docker Resources",
        icon: IconProp.Cube,
        tableDescription: "Snapshot of a Docker object (container, image, network, volume) as last reported by the OneUptime Docker Agent. Populated by the telemetry ingest pipeline; not user-editable.",
    }),
    Index(["projectId", "dockerHostId", "kind", "name"], {
        unique: true,
    }),
    Entity({
        name: "DockerResource",
    })
], DockerResource);
export default DockerResource;
//# sourceMappingURL=DockerResource.js.map