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

export type TrainingPlanDocument = TrainingPlan & Document;

@Schema({ _id: false })
export class TrainingSet {
  // Dynamic numeric values keyed by the category's FieldDefinition `key`.
  // e.g. strength -> { reps: 12, weight: 20, break_duration: 60 }
  //      time     -> { duration: 60, break_duration: 30 }
  //      cardio   -> { distance: 1.5, duration: 600 }
  @Prop({ type: MongooseSchema.Types.Mixed, default: {} })
  values: Record<string, number>;
}

export const TrainingSetSchema = SchemaFactory.createForClass(TrainingSet);

@Schema({ _id: false })
export class PlanExercise {
  @Prop({ type: Types.ObjectId, ref: "Exercise", required: true })
  exercise_id: Types.ObjectId;

  @Prop({ type: [TrainingSetSchema], default: [] })
  sets: TrainingSet[];
}

export const PlanExerciseSchema = SchemaFactory.createForClass(PlanExercise);

// _id enabled so every day has a stable ObjectId — used to identify a day when
// fetching and to mark it done/not-done against workout sessions.
@Schema({ _id: true })
export class PlanDay {
  @Prop({ type: Object, required: true })
  name: Record<string, string>; // e.g. { en: "Day 1", de: "Tag 1" } or { en: "Upper Body", de: "Oberkörper" }

  @Prop({ type: [PlanExerciseSchema], default: [] })
  exercises: PlanExercise[];
}

export const PlanDaySchema = SchemaFactory.createForClass(PlanDay);

@Schema({ timestamps: true, strict: true })
export class TrainingPlan {
  @Prop({ type: Object, required: true })
  name: Record<string, string>;

  @Prop({ type: Object })
  description: Record<string, string>;

  // Short one-liner shown on the plan card (below the name).
  @Prop({ type: Object })
  subtitle: Record<string, string>;

  @Prop({ type: [PlanDaySchema], default: [] })
  days: PlanDay[];

  // full URL of the plan image, uploaded via FileStorageService
  @Prop({ type: String, default: null })
  attachment: string | null;

  @Prop({ default: 1 })
  status: number;

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

  @Prop({
    type: Types.ObjectId,
    ref: "User",
    required: true,
  })
  createdBy: Types.ObjectId;

  // Whether admin or user created it
  @Prop({
    type: String,
    enum: ["admin", "user"],
    default: "user",
  })
  createdByType: string;



}

export const TrainingPlanSchema = SchemaFactory.createForClass(TrainingPlan);
