'use strict';
/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
  Schema = mongoose.Schema;
/*
 * Infrastructure Requests have these categoeries:
 * 1) OtherRequest
 *  a) Equipment
 *  b) Feature
 *  c) Process
 * 2) Hosting
 *  a) New Hosting
 *  b) Modify Hosting
 *  c) Decomm Hosting
 *
 */

var AddressSchema = new Schema({    
  commonName: {
    type: String,
    default: '',
    trim: true,
    uppercase: true
  },
  street1: {
    type: String,
    default: '',
    trim: true,
    uppercase: true
  },
  street2: {
    type: String,
    default: '',
    trim: true,
    uppercase: true
  },
  floor: {
    type: String,
    default: '',
    trim: true
  },
  section: {
    type: String,
    default: '',
    trim: true
  },
  city: {
    type: String,
    default: '',
    trim: true,
    uppercase: true
  },
  state: {
    type: String,
    default: '',
    trim: true,
    uppercase: true
  },
  postalCode: {
    type: String,
    default: '',
    trim: true
  },
  country: {
    type: String,
    default: '',
    trim: true,
    uppercase: true
  },  
  clli: {
    type: String,
    default: '',
    trim: true,
    uppercase: true
  }
});

////mongoose.model('Address', AddressSchema);

var AuditInfoSchema = new Schema ({
  authorID: {
    type: String,
    default: '',
    trim: true
  },
  firstName: {
    type: String,
    default: '',
    trim: true
  },
  lastName: {
    type: String,
    default: '',
    trim: true
  },
  entryTime: {
    type: String,
    default: '',
    trim: true
  },
  lifeCyclePhase: {
    type: String,
    default: '',
    trim: true
  },
  version: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('AuditInfo', AuditInfoSchema);

var ComputeSchema = new Schema ({
  vCPU: {
    type: String,
    default: '',
    trim: true
  },
  CPU: {
    type: String,
    default: '',
    trim: true
  },
  RAM: {
    type: String,
    default: '',
    trim: true
  },
  certOS: {
    type: String,
    default: '',
    trim: true
  },
  certOSVer: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('Compute', ComputeSchema);

var DecommHostingSchema = new Schema ({
  targetName: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('DecommHosting', DecommHostingSchema);

var DependencySchema = new Schema ({
/*
Examples:
ReasonType=NetworkReason
Reason="Application requires OAM network access"

ReasonType=ApplicationReason
Reason="Application requires colocation with CRM app in Alpharetta"

ReasonType=OtherReason
Reason="Project adding additional servers to existing application in Birmingham"
 */
  reason: {
    type: String,
    default: '',
    trim: true
  },
  reasonType: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('Dependency', DependencySchema);

var EquipmentSchema = new Schema ({
  name: {
    type: String,
    default: '',
    trim: true
  },
  ALMID: {
    type: String,
    default: '',
    trim: true
  },
  instanceCount: {
    type: Number,
    default: 0
  },
  note: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('Equipment', EquipmentSchema);
var TargetSchema = new Schema ({
  hostName: {
    type: String,
    default: '',
    trim: true
  },
  port: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('Target', TargetSchema);

var FeatureSchema = new Schema ({
  name: {
    type: String,
    default: '',
    trim: true
  },
  action: {
    type: String,
    default: '',
    trim: true
  },
  description: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  },
  targets: [TargetSchema]
});

//mongoose.model('Feature', FeatureSchema);

var MgtModelSchema = new Schema ({
/*
 * GCSOManaged, SelfManaged, CPDSManaged
 */
  type: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('MgtModel', MgtModelSchema);

var NetworkGatewaySchema = new Schema ({
/*
 * Examples:
 * Name=MobilityOAM
 * Description=Admin netwoirk for Mobility backplane
 * Type=Intranet
 *
 * Name=CBB_EBR
 * Type=Intranet
 */
  name: {
    type: String,
    default: '',
    trim: true
  },
  description: {
    type: String,
    default: '',
    trim: true
  },
  type: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('NetworkGateway', NetworkGatewaySchema);

var PackageSchema = new Schema ({
/* Product Offer Package
Especially useful prior to order stage.  Estimates may only have product information without location information used to identify hosting types.
Scenario - estimate based on MDIE that has a location of Any and a list of generic workloads.  That is the vague information available for initial storage of the request.  This can be retrieved later to convert to an order.
This supports the life cycle of hosting requirements.

Examples:
PackageKey=Name
PackageValue=Linux Only XL64

PackageKey=Name
PackageValue=Oracle2T
*/
  offeringName: {
    type: String,
    default: '',
    trim: true
  },
  offeringVersion: {
    type: String,
    default: '',
    trim: true
  },
  instanceCt: {
    type: Number,
    default:0
  }          
});

//mongoose.model('Package', PackageSchema);

var PayloadSchema = new Schema ({
/*
 * Workloads contain payloads.  Payloads are the members of the software stack in a workload (Typically tools)
 */
  containerSW: {
    type: String,
    default: '',
    trim: true
  },
  containerSWVer: {
    type: String,
    default: '',
    trim: true
  },
  NLSCharSet: {
    type: String,
    default: '',
    trim: true
  },
  NLSNCharCharSet: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('Payload', PayloadSchema);

var ArgumentListSchema = new Schema ({
  UID: {
    type: String,
    default: '',
    trim: true
  },
  hostName: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('ArgumentList', ArgumentListSchema);

var ProcessSchema = new Schema ({
  handler: {
    type: String,
    default: '',
    trim: true
  },
  name: {
    type: String,
    default: '',
    trim: true
  },
  ALMTask: {
    type: String,
    default: '',
    trim: true
  },
  argumentLists: [ArgumentListSchema],
  note: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('Process', ProcessSchema);

var ProjectMgtSchema = new Schema ({
  application: {
    type: String,
    default: '', trim: true
  },
  name: {
    type: String,
    default: '',
    trim: true
  },
  ID: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('ProjectMgt', ProjectMgtSchema);

var RelatedApplicationSchema = new Schema ({
  MOTSID: {
    type: String,
    default: '',
    trim: true
  },
  MOTSAcronym: {
    type: String,
    default: '',
    trim: true
  },
  MOTSName: {
    type: String,
    default: '',
    trim: true
  }
});

////mongoose.model('RelatedApplication', RelatedApplicationSchema);

var SecuritySchema = new Schema ({
  handles: {
    type: String,
    default: '',
    trim: true
  },
  stores: {
    type: String,
    default: '',
    trim: true
  },
  other: {
    type: String,
    default: '',
    trim: true
  }
});

////mongoose.model('Security', SecuritySchema);

var StorageSchema = new Schema ({
  type: {
    type: String,
    default: '',
    trim: true
  },
  mgAmount: {
    type: String,
    default: '',
    trim: true
  },
  fileSystemPath: {
    type: String,
    default: '',
    trim: true
  }
});

//mongoose.model('Storage', StorageSchema);

var SiteSchema = new Schema ({
  type: {
    type: String,
    default: '',
    trim: true
  },
  description: {
    type: String,
    default: '',
    trim: true
  },
  zone: {
    type: String,
    default: '',
    trim: true
  },
  cluster: {
    type: String,
    default: '',
    trim: true
  },
  address: AddressSchema,
  dependencies: [DependencySchema]
});

//mongoose.model('Site', SiteSchema);

var TenantQuotaSchema = new Schema ({
  name: {
    type: String,
    default: '',
    trim: true
  },
  description: {
    type: String,
    default: '',
    trim: true
  },
  flavor: {
    type: String,
    default: '',
    trim: true
  },
  type: {
    type: String,
    default: '',
    trim: true
  },
  sites:[SiteSchema],
  computes: [ComputeSchema],
  storages: [StorageSchema]
});

//mongoose.model('TenantQuota', TenantQuotaSchema);

var WorkloadSchema = new Schema ({
/*
 * Attributes for workload running in a host
 */
  instanceCount: {
    type: Number,
    default:0
  },
  description: {
    type: String,
    default: '',
    trim: true
  },
  BMLRequired: {
    type: String,
    default: '',
    trim: true
  },
  payloads: [PayloadSchema],
  IOPSMaxRequired: {
    type: Number,
    default: '',
    trim: true
  },
  mgtModels: [MgtModelSchema],
  networkGateways: [NetworkGatewaySchema],
  hypervisor: {
    type: String,
    default: '',
    trim: true
  },
  securitys: [SecuritySchema],
  sites: [SiteSchema],
  computes: [ComputeSchema],
  storages: [StorageSchema]
});

//mongoose.model('Workload', WorkloadSchema);

var EnvironmentSchema = new Schema({
  /*
   * Example:
    
  SupportClass=Prod
  EnvironmentName=Prod1 or Test3 or Dev1 or UAT
  Description=Oversized Superfluous Performance Test environment required by vendor
  */
  supportClass: {
    type: String,
    default: '',
    trim: true
  },
  name: {
    type: String,
    default: '',
    trim: true
  },
  description: {
    type: String,
    default: '', trim: true
  },
  tenantQuotas: [TenantQuotaSchema],
  workloads: [WorkloadSchema]
});

//mongoose.model('Environment', EnvironmentSchema);

var ModifyHostingSchema = new Schema({
  /*
   * Only tenants and hosts (guests) can have their attibutes modified.
   * This requires a target ID and new attribute values for infrastrucute items addressed by TP&E
  */
  targetName: {
    type: String,
    default: '',
    trim: true
  },
  description: {
    type: String,
    default: '',
    trim: true
  },
  computes: [ComputeSchema],
  storages: [StorageSchema]  
});

//mongoose.model('ModifyHosting', ModifyHostingSchema);

var NewHostingSchema = new Schema ({
  packages: [PackageSchema],
  relatedApplications: [RelatedApplicationSchema],  
  environments: [EnvironmentSchema]
});

//mongoose.model('NewHosting', NewHostingSchema);

var OtherRequestSchema = new Schema ({
  features: [FeatureSchema],
  equipments: [EquipmentSchema],
  processes: [ProcessSchema]
});

//mongoose.model('OtherRequest', OtherRequestSchema);

var HostingSchema = new Schema ({
  type: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  },
  newHostings: [NewHostingSchema],
  modifyHostings: [ModifyHostingSchema],
  decommHostings: [DecommHostingSchema]
});

//mongoose.model('Hosting', HostingSchema);

var infrastructureRequestSchema = new Schema ({
/*
 * Highest Level
Infrastructure Item(s) requested in Order - Can be Hosting or Non-Hosting

Examples
RequestSource=AIC Self Serve Application
RequestID=12345678

RequestSource=EOE Order Application
RequestID=12345678
Note=Ordering package for new employee

 */
  creationDate: {
    type: Date,
    default: Date.now
  },
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  requestSource: {
    type: String,
    default: '',
    trim: true
  },
  requestID: {
    type: String,
    default: '',
    trim: true
  },
  note: {
    type: String,
    default: '',
    trim: true
  },
  hostings: [HostingSchema],
  otherRequests: [OtherRequestSchema]
});

mongoose.model('InfrastructureRequest', infrastructureRequestSchema);
