import { ApiPropertyOptional } from '@nestjs/swagger';
import {
  ArrayMaxSize,
  IsArray,
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  MaxLength,
} from 'class-validator';
import { ModeOfJoiningEnum } from 'src/common/enum/mode-of-joining.enum';
import { DeviceTypeEnum } from 'src/common/enum/device-type.enum';
import { MAX_JOIN_COMPANIONS } from 'src/common/constants/online-attendance.constants';

/**
 * Optional client metadata sent when a participant clicks "join". Stored on the
 * attendance row and merged into analytics during reconciliation. All optional so
 * existing clients that send an empty body keep working.
 */
export class JoinSessionDto {
  /**
   * The registration joining this session. A registration is per-program and one
   * user can hold several (proxy/child), so the join is attributed to an explicit
   * registration rather than the logged-in user. Falls back to the authenticated
   * user's own registration when omitted.
   */
  @ApiPropertyOptional({ description: 'Registration id joining the session' })
  @IsOptional()
  @IsInt()
  registrationId?: number;

  @ApiPropertyOptional({ enum: ModeOfJoiningEnum })
  @IsOptional()
  @IsEnum(ModeOfJoiningEnum)
  modeOfJoining?: ModeOfJoiningEnum;

  @ApiPropertyOptional({ enum: DeviceTypeEnum })
  @IsOptional()
  @IsEnum(DeviceTypeEnum)
  deviceType?: DeviceTypeEnum;

  @ApiPropertyOptional({ description: "Free-form client detail (e.g. 'ios', 'android', user-agent)" })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  deviceInfo?: string;

  /**
   * Registration ids of other people physically present who joined through this
   * device. Each must be a confirmed registration of the SAME session to be
   * credited; others are skipped. Identified by registration (not user) so proxy
   * and child registrations are handled correctly. Used for "join with others".
   */
  @ApiPropertyOptional({
    type: [Number],
    description: 'Registration ids of co-located companions to credit',
  })
  @IsOptional()
  @IsArray()
  @ArrayMaxSize(MAX_JOIN_COMPANIONS)
  @IsInt({ each: true })
  companionRegistrationIds?: number[];
}
