import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { Document, Types } from "mongoose";

export type PerformanceDocument = Performance & Document;

// One document per user + exercise + date — a single personal record entry.
// All metric fields are optional; only the relevant one(s) are sent based on
// the exercise category (Weight & Reps, Time/Hold, Distance & Time, etc.).
@Schema({ timestamps: true, strict: true })
export class Performance {
  @Prop({ type: Types.ObjectId, ref: "User", required: true })
  userId!: Types.ObjectId;

  @Prop({ type: Types.ObjectId, ref: "Exercise", required: true })
  exercise_id!: Types.ObjectId;

  @Prop({ required: true })
  date!: Date;

  @Prop({ type: Number, default: null }) // Weight & Reps, Reps Only, Assisted BW, Weighted BW
  reps!: number | null;

  @Prop({ type: Number, default: null }) // Weight & Reps (kg)
  weight!: number | null;

  @Prop({ type: Number, default: null }) // Time/Hold, Distance & Time (seconds)
  duration!: number | null;

  @Prop({ type: Number, default: null }) // Distance & Time (km)
  distance!: number | null;

  @Prop({ type: Number, default: null }) // Assisted Bodyweight (negative kg)
  assisted_weight!: number | null;

  @Prop({ type: Number, default: null }) // Weighted Bodyweight (positive kg)
  added_weight!: number | null;

  @Prop({ default: null })
  deleted_at!: Date | null;
}

export const PerformanceSchema = SchemaFactory.createForClass(Performance);
// Compound index for fast per-exercise history and per-date lookups
PerformanceSchema.index({ userId: 1, exercise_id: 1, date: 1 });
PerformanceSchema.index({ userId: 1, date: 1 });
