// media-worker.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const STATUS_LIST = [
  'starting',   // the router process is starting and configuring itself
  'active',     // the router is active and available for service
  'capacity',   // the router is at or over capacity
  'closing',    // the router is closing/shutting down
  'closed',     // the router no longer exists
];

const WebRtcListenSchema = new Schema({
  protocol: { type: String, enum: ['tcp','udp'], required: true },
  ip: { type: String, required: true },
  port: { type: Number, required: true },
});

/*
 * A media worker is a host process with one or more MediaRouter instances
 * processing multi-user conference calls.
 */
const MediaWorkerSchema = new Schema({
  created: { type: Date, default: Date.now, required: true, expires: '30d' },
  lastActivity: { type: Date, default: Date.now, required: true },
  status: { type: String, enum: STATUS_LIST, default: 'starting', required: true, index: true },
  webRtcServer: {
    listenInfos: { type: [WebRtcListenSchema] },
  },
  stats: {
    routerCount: { type: Number, default: 0, required: true },
    consumerCount: { type: Number, default: 0, required: true },
    producerCount: { type: Number, default: 0, required: true },
  }
});

module.exports = mongoose.model('MediaWorker', MediaWorkerSchema);