import * as CommonSchemas from './mongoose-common-schemas';
// import * as ScenarioSchemas from './mongoose-scenario-schemas';
import { Document, Schema } from 'mongoose';
import { FeatureResults, FeatureResultsOverview } from '../Report/Results';
import { Feature } from '../Report/Feature';

export interface MFeature extends Feature, Document {
  id: any;
  _id: any;
}

export interface MFeatureResults extends FeatureResults, Document {}
export interface MFeatureResultsOverview extends FeatureResultsOverview, Document {}

export const featureResultsOverviewSchema = new Schema( {
  duration: { required: false, type: Number },
  durationHHMMSS: { required: false, type: String },
  status: [{ required: false, type: String }],
  statusIcons: [{ required: false, type: String }],
},{
  _id: false,
  id: false,
} );

export const featureResultsSchema = new Schema( {
  overview: featureResultsOverviewSchema,
  scenarios: CommonSchemas.moduleResultsSchema,
  steps: CommonSchemas.moduleResultsSchema,
},{
  _id: false,
  id: false,
} );

export const featureSchema = new Schema( {
  // Index: { required: false, type: Number },
  ObjectId: { required: false, type: String },
  description: { required: false , type: String },
  // elements: [{ required: false, type:ScenarioSchemas.scenarioSchema }],
  // elements: [{ ref: 'Scenario', required: false, type: ScenarioSchemas.scenarioSchema }],
  id: { required: true, type: String },
  keyword: { required: true, type: String },
  line: { required: true, type: Number },
  metadata: [{ required: false, type: CommonSchemas.metadataSchema }],
  name: { required: true, type: String },
  reportId: { ref: 'Report', type: Schema.Types.ObjectId },
  results: featureResultsSchema,
  tags: [CommonSchemas.tagSchema],
  uri: { required: true, type: String },
}, {
  // _id: false,
  // id: false,
  toJSON: {
    transform: ( ret: Feature ) =>{
      delete ret._id;
    },
    versionKey: false,
    virtuals: true
  },
  toObject: {
    versionKey: false,
    virtuals: true
  }
} );

featureSchema.virtual( 'elements',{
  foreignField: 'featureId',
  justOne: false,
  localField: '_id',
  ref: 'Scenario',
} );
