// src/models/drone-monitor.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved

import { Schema, model } from "mongoose";
import { IDroneMonitor, IMemoryMonitor } from "@gadget/api";
import { nanoid } from "nanoid";

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

export const DroneMonitorSchema = new Schema<IDroneMonitor>({
  _id: { type: String, default: () => nanoid() },
  registration: { 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 DroneMonitor = model<IDroneMonitor>(
  "DroneMonitor",
  DroneMonitorSchema,
);
export default DroneMonitor;

(async () => {
  await DroneMonitor.syncIndexes();
})();
