// src/models/host-monitor.ts
// Copyright (C) 2025 DTP Technologies, LLC
// All Rights Reserved

import { Types, Schema, Document, model } from "mongoose";

export interface IMemoryMonitor {
  count: number;
  bytes: number;
}

export const MemoryMonitorSchema = new Schema<IMemoryMonitor>({
  count: { type: Number, default: 0, required: true },
  bytes: { type: Number, default: 0, required: true },
});

export interface IHostMonitor extends Document {
  _id: Types.ObjectId;
  hostname: string;
  timestamp: Date;
  memory: {
    rss: number;
    v8: {
      heapTotal: number;
      heapUsed: number;
      heapExternal: number;
    };
    os: {
      total: number;
      free: number;
    };
    ai: {
      subagents: IMemoryMonitor;
      fileOperations: IMemoryMonitor;
      toolCalls: IMemoryMonitor;
    };
    logs: IMemoryMonitor;
  };
}

export const HostMonitorSchema = new Schema<IHostMonitor>({
  hostname: { type: String, required: true, index: 1 },
  timestamp: { type: Date, required: true, index: -1 },
  memory: {
    rss: { type: Number, required: true },
    v8: {
      heapTotal: { type: Number, required: true },
      heapUsed: { type: Number, required: true },
      heapExternal: { type: Number, required: true, default: 0 },
    },
    os: {
      total: { type: Number, required: true },
      free: { type: Number, required: true },
    },
    ai: {
      subagents: { type: MemoryMonitorSchema, required: true },
      fileOperations: { type: MemoryMonitorSchema, required: true },
      toolCalls: { type: MemoryMonitorSchema, required: true },
    },
    logs: { type: MemoryMonitorSchema, required: true },
  },
});

export const HostMonitor = model<IHostMonitor>(
  "HostMonitor",
  HostMonitorSchema,
);
export default HostMonitor;
