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

export type ExerciseCategoryDocument = ExerciseCategory & Document;

export class FieldDefinition {
  key!: string;
  label!: Record<string, string>;
  unit!: string;
  type!: string;
}

// Must use an explicit MongooseSchema here because 'type' is a reserved
// keyword in Mongoose — using it inside a plain @Prop object literal
// causes Mongoose to treat the whole subdocument as a String cast.
const FieldDefinitionSchema = new MongooseSchema(
  {
    key:   { type: String, required: true },
    label: { type: Object, required: true },
    unit:  { type: String, default: "" },
    type:  { type: String, required: true },
  },
  { _id: false },
);

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

  @Prop({ type: [FieldDefinitionSchema], default: [] })
  fields!: FieldDefinition[];

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

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

export const ExerciseCategorySchema = SchemaFactory.createForClass(ExerciseCategory);
