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

export type ManualPerformanceDocument = ManualPerformance & Document;

// One document per user + exercise + date — a single best set entry.
// The client may send several sets; only the strongest one is stored, flattened
// onto the document. All metric fields are optional; only the relevant one(s)
// are populated based on the exercise category.
@Schema({ timestamps: true, strict: true })
export class ManualPerformance {
  @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 ManualPerformanceSchema =
  SchemaFactory.createForClass(ManualPerformance);

// Compound index for fast per-exercise history and per-date lookups
ManualPerformanceSchema.index({ userId: 1, exercise_id: 1, date: 1 });
ManualPerformanceSchema.index({ userId: 1, date: 1 });
