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

import { Schema, model } from "mongoose";
import { DroneStatus, IDroneRegistration } from "@gadget/api";
import { nanoid } from "nanoid";

export const DroneRegistrationSchema = new Schema<IDroneRegistration>({
  _id: { type: String, default: () => nanoid() },
  createdAt: { type: Date, required: true },
  updatedAt: { type: Date, required: false },
  user: { type: String, ref: "User", required: true },
  hostname: { type: String, required: true },
  workspaceDir: { type: String, required: true },
  status: {
    type: String,
    enum: DroneStatus,
    default: DroneStatus.Starting,
    required: true,
  },
  chatSessionId: { type: String, required: false },
  currentJobId: { type: String, required: false },
});

DroneRegistrationSchema.index({
  user: 1,
  status: 1,
});

export const DroneRegistration = model<IDroneRegistration>(
  "DroneRegistration",
  DroneRegistrationSchema,
);
export default DroneRegistration;

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