import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

export type RouteSegmentDocument = HydratedDocument<RouteSegment>;

export type RouteSegmentKind = 'gps_verified' | 'map_matched' | 'estimated';

@Schema({ timestamps: true, collection: 'route_segments' })
export class RouteSegment {
  @Prop({ required: true, index: true })
  requestId: string;

  @Prop({ required: true, index: true })
  tripDbId: string;

  @Prop({ required: true })
  segId: string;

  @Prop({ index: true })
  legId?: string;

  @Prop({ required: true })
  kind: RouteSegmentKind;

  @Prop({ required: true, default: 0 })
  confidence: number;

  @Prop({ required: true, default: 0 })
  lengthM: number;

  @Prop()
  fromTimestamp?: Date;

  @Prop()
  toTimestamp?: Date;

  /** Pipe polyline: lat,lng|lat,lng|... */
  @Prop({ required: true, default: '' })
  polylineEncoded: string;

  @Prop()
  matchMethod?: string;

  @Prop({ index: true })
  matchJobId?: string;
}

export const RouteSegmentSchema = SchemaFactory.createForClass(RouteSegment);
RouteSegmentSchema.index({ requestId: 1, segId: 1 }, { unique: true });
RouteSegmentSchema.index({ tripDbId: 1, createdAt: -1 });
