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

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

export enum DroneStatus {
  Online = "online",
  Offline = "offline",
}

export interface IDrone extends Document {
  _id: Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
  user: Types.ObjectId;
  ip: string;
  port: number;
  status: DroneStatus;
}

const DroneSchema = new Schema<IDrone>({
  createdAt: { type: Date, required: true },
  updatedAt: { type: Date, required: true },
  user: { type: Schema.Types.ObjectId, ref: "User", index: 1 },
  status: {
    type: String,
    enum: DroneStatus,
    default: DroneStatus.Online,
    required: true,
  },
  ip: { type: String, required: true },
  port: { type: Number, required: true },
});

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

export const Drone = model<IDrone>("Drone", DroneSchema);
export default Drone;

// Note: Index synchronization is now handled during application startup
// to ensure the database connection is established first.
// See src/lib/db.ts for the syncDatabaseIndexes function.
