import * as CommonSchemas from './mongoose-common-schemas';
import { Document, Schema } from 'mongoose';
import { ReportResults, ReportResultsOverview } from '../Report/Results';
import { metadataSchema, statusSchema } from '../Mongoose/mongoose-common-schemas';
import { ExtendedReport } from '../Report/ExtendedReport';
// import { featureSchema } from '../Mongoose/mongoose-feature-schemas';

export interface MExtendedReport extends ExtendedReport, Document {
  id: any;
  _id: any;
}
export interface MReportResultsOverview extends ReportResultsOverview, Document {
  _id: any;
}
export interface MReportResults extends ReportResults, Document {
  _id: any;
}

export const reportResultsOverviewSchema = new Schema( {
  Index: { required: false, type: Number },
  date: { required: true, type: Date },
  duration: { required: true, type: Number },
  durationHHMMSS: { required: true, type: String },
  executionDate: { required: true, type: String },
  executionTime: { required: true, type: String },
  metadata: [{ required: false, type: metadataSchema }],
  name: { required: false, type: String },
  status: { required: true, type: statusSchema },
  version: { required: false, type: String },
},{
  _id: false,
  id: false,
} );

export const reportResultsSchema = new Schema( {
  features: { required: true, type: CommonSchemas.moduleResultsSchema },
  overview: { required: true, type: reportResultsOverviewSchema },
  scenarios: { required: true, type: CommonSchemas.moduleResultsSchema },
  steps: { required: true, type: CommonSchemas.moduleResultsSchema },
} ,{
  _id: false,
  id: false,
} );

export const reportsSchema = new Schema( {
  // _id: { required: true, type: String },
  // features: [{ required: false , type: featureSchema }],
  // id: { required: false, type: String },
  metadata: [{ required: true, type: CommonSchemas.metadataSchema } ],
  results: { required:true, type: reportResultsSchema }
}, {
  // _id: false,
  id: false,
  toJSON: {
    // transform: ( ret: ExtendedReport ) =>{
    //  delete ret._id;
    // },
    versionKey: false,
    virtuals: true
  },
  toObject: {
    versionKey: false,
    virtuals: true
  }
} );

reportsSchema.virtual( 'features',{
  foreignField: 'reportId',
  justOne: false,
  localField: '_id',
  ref: 'Feature',
} );
