import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';
import { TrackingEventType } from '../../common/constants/enums';

export type TrackingEventDocument = HydratedDocument<TrackingEvent>;

@Schema({
  timestamps: { createdAt: true, updatedAt: false },
  collection: 'tracking_events',
})
export class TrackingEvent {
  @Prop({ required: true, index: true })
  requestId: string;

  @Prop({ required: true, index: true })
  tripDbId: string;

  @Prop()
  legId?: string;

  @Prop()
  sessionId?: string;

  @Prop({ type: String, enum: TrackingEventType, required: true })
  type: TrackingEventType;

  @Prop({ required: true, index: true })
  timestamp: Date;

  @Prop({ type: Object })
  metadata?: {
    lastPointAt?: string;
    reason?: string;
    platform?: string;
  };

  @Prop({ required: true, index: true })
  dedupeKey: string;
}

export const TrackingEventSchema = SchemaFactory.createForClass(TrackingEvent);
TrackingEventSchema.index({ requestId: 1, dedupeKey: 1 }, { unique: true });
TrackingEventSchema.index({ tripDbId: 1, timestamp: 1 });
