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

// A single set the client entered. The client may send several of these per
// exercise; the service keeps only the strongest one (see pickBestSet).
export class ManualPerformanceSetDto {
  @ApiPropertyOptional({ description: "Reps (Weight & Reps, Reps Only, Assisted BW, Weighted BW)" })
  @IsOptional()
  @IsNumber()
  reps?: number;

  @ApiPropertyOptional({ description: "Weight in kg (Weight & Reps)" })
  @IsOptional()
  @IsNumber()
  weight?: number;

  @ApiPropertyOptional({ description: "Duration in seconds (Time/Hold, Distance & Time)" })
  @IsOptional()
  @IsNumber()
  duration?: number;

  @ApiPropertyOptional({ description: "Distance in km (Distance & Time)" })
  @IsOptional()
  @IsNumber()
  distance?: number;

  @ApiPropertyOptional({ description: "Assistance in kg — negative value (Assisted Bodyweight)" })
  @IsOptional()
  @IsNumber()
  assisted_weight?: number;

  @ApiPropertyOptional({ description: "Added load in kg — positive value (Weighted Bodyweight)" })
  @IsOptional()
  @IsNumber()
  added_weight?: number;
}

// One exercise = its id plus the sets logged for it. Only the best set is stored.
export class ManualPerformanceExerciseDto {
  @ApiProperty({ description: "Exercise ID" })
  @IsMongoId()
  exercise_id: string;

  @ApiProperty({
    type: [ManualPerformanceSetDto],
    description: "One or more sets — only the strongest set is saved",
  })
  @IsArray()
  @ArrayMinSize(1)
  @ValidateNested({ each: true })
  @Type(() => ManualPerformanceSetDto)
  sets: ManualPerformanceSetDto[];
}

export class CreateManualPerformanceDto {
  @ApiProperty({ example: "2025-06-29", description: "Date of the performance session" })
  @IsDateString()
  date: string;

  @ApiProperty({
    type: [ManualPerformanceExerciseDto],
    description: "One or more exercises performed on this date — each saved as a separate document",
  })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ManualPerformanceExerciseDto)
  exercises: ManualPerformanceExerciseDto[];
}
