import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import {
  IsArray,
  IsDateString,
  IsMongoId,
  IsObject,
  IsOptional,
  IsString,
  ValidateNested,
} from "class-validator";
import { Type } from "class-transformer";

export class WorkoutSetDto {
  // Dynamic values keyed by the exercise category's field keys.
  // e.g. { reps: 12, weight: 20, break_duration: 60 } or { duration: 60, break_duration: 30 }
  @ApiProperty({
    example: { reps: 12, weight: 20, break_duration: 60 },
  })
  @IsObject()
  values: Record<string, number | string>;
}

export class WorkoutExerciseDto {
  @ApiProperty({ example: "6a33769101dc529df9f7f934" })
  @IsMongoId()
  exercise_id: string;

  @ApiProperty({ type: [WorkoutSetDto] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => WorkoutSetDto)
  sets: WorkoutSetDto[];
}

export class CreateWorkoutSessionDto {
  @ApiProperty({ example: "6a3a29bf460cefd8da745fc5" })
  @IsMongoId()
  trainingPlan: string;

  @ApiProperty({ example: "6a3a29bf460cefd8da745fc6" })
  @IsMongoId()
  dayId: string;

  @ApiProperty({ example: "2026-06-23T06:37:51.775Z" })
  @IsDateString()
  startedAt: string;

  // Required: duration is calculated server-side from startedAt -> endedAt.
  @ApiProperty({ example: "2026-06-23T07:22:51.775Z" })
  @IsDateString()
  endedAt: string;

  @ApiPropertyOptional({ example: "Felt strong today" })
  @IsOptional()
  @IsString()
  notes?: string;

  @ApiProperty({ type: [WorkoutExerciseDto] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => WorkoutExerciseDto)
  exercises: WorkoutExerciseDto[];
}
