import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

export type MatchJobDocument = HydratedDocument<MatchJob>;

export type MatchReason = 'catch_up' | 'trip_end' | 'incremental' | 'manual';
export type MatchJobStatus = 'pending' | 'ready' | 'failed';

@Schema({ timestamps: true, collection: 'match_jobs' })
export class MatchJob {
  @Prop({ required: true, unique: true, index: true })
  jobId: string;

  @Prop({ required: true, index: true })
  requestId: string;

  @Prop({ required: true, index: true })
  tripDbId: string;

  @Prop({ required: true, default: 'pending' })
  status: MatchJobStatus;

  @Prop({ required: true })
  reason: MatchReason;

  @Prop({ default: 'google_roads' })
  engine: string;

  @Prop()
  error?: string;

  @Prop()
  finishedAt?: Date;

  @Prop({ type: Object })
  resultSummary?: Record<string, unknown>;
}

export const MatchJobSchema = SchemaFactory.createForClass(MatchJob);
MatchJobSchema.index({ requestId: 1, createdAt: -1 });
