import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';
import { FuelType, TripStatus, VehicleType } from '../../common/constants/enums';

export type TripDocument = HydratedDocument<Trip>;

@Schema({ timestamps: true, collection: 'trips' })
export class Trip {
  @Prop({ required: true, unique: true, index: true })
  tripId: string;

  @Prop({ required: true, index: true })
  userId: string;

  @Prop({ default: Date.now, index: true })
  requestDate: Date;

  @Prop()
  city?: string;

  @Prop()
  fromLocation?: string;

  @Prop()
  toLocation?: string;

  @Prop()
  originLat?: number;

  @Prop()
  originLng?: number;

  @Prop()
  clientName?: string;

  @Prop()
  destinationLat?: number;

  @Prop()
  destinationLng?: number;

  @Prop()
  googlePlaceId?: string;

  @Prop()
  plannedDistance?: number;

  @Prop({ type: String, enum: VehicleType })
  vehicleType?: VehicleType;

  @Prop({ type: String, enum: FuelType })
  fuelType?: FuelType;

  @Prop()
  purpose?: string;

  @Prop({ type: String, enum: TripStatus, default: TripStatus.CREATED, index: true })
  status: TripStatus;

  @Prop({ default: 0 })
  totalDistance: number;

  @Prop({ default: 0 })
  travelDuration: number;

  @Prop({ default: 0 })
  idleTime: number;

  @Prop({ default: 0 })
  meetingDuration: number;

  @Prop()
  productivityScore?: number;

  @Prop()
  tripEfficiency?: number;

  @Prop()
  startedAt?: Date;

  @Prop()
  arrivedAt?: Date;

  @Prop()
  completedAt?: Date;

  @Prop({ type: [Object], default: [] })
  tripLegs?: Record<string, unknown>[];

  @Prop({ type: Number, default: 0 })
  currentLegIndex?: number;
}

export const TripSchema = SchemaFactory.createForClass(Trip);
TripSchema.index({ userId: 1, status: 1 });
TripSchema.index({ userId: 1, createdAt: -1 });
TripSchema.index({ userId: 1, requestDate: -1 });
