import { Type } from 'class-transformer';
import {
  ArrayMinSize,
  IsArray,
  IsInt,
  IsNotEmpty,
  IsObject,
  IsString,
  MaxLength,
  ValidateNested,
} from 'class-validator';

export class UpsertProgressDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(128)
  anonymousSessionId!: string;

  @IsString()
  @IsNotEmpty()
  @MaxLength(128)
  entityType!: string;

  @IsInt()
  entityId!: number;

  /** completed, timeSpentSeconds, score, lastPosition, completedAt, ... */
  @IsObject()
  progressJson!: Record<string, any>;
}

export class ProgressEntryDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(128)
  entityType!: string;

  @IsInt()
  entityId!: number;

  @IsObject()
  progressJson!: Record<string, any>;
}

/** Batch upload of progress accumulated while the device was offline. */
export class SyncProgressDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(128)
  anonymousSessionId!: string;

  @IsArray()
  @ArrayMinSize(1)
  @ValidateNested({ each: true })
  @Type(() => ProgressEntryDto)
  entries!: ProgressEntryDto[];
}
