import {
  IsString,
  IsOptional,
  IsNumber,
  IsUUID,
  IsObject,
  MaxLength,
  ValidateNested,
  ArrayMaxSize,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

export class TelemetryEventItem {
  @ApiProperty({ example: 'page_view', maxLength: 64 })
  @IsString() @MaxLength(64)
  eventName: string;

  @ApiProperty({ example: 'inst_abc123', maxLength: 128 })
  @IsString() @MaxLength(128)
  installId: string;

  @ApiPropertyOptional()
  @IsOptional() @IsUUID()
  userId?: string;

  /** Unix 时间戳（秒） */
  @ApiProperty({ example: 1709644800 })
  @IsNumber()
  clientTs: number;

  // 设备字段：顶层独立列，方便服务端按索引查询
  @ApiPropertyOptional({ example: 'Xiaomi', maxLength: 64 })
  @IsOptional() @IsString() @MaxLength(64)
  deviceBrand?: string;

  @ApiPropertyOptional({ example: 'Redmi Note 12', maxLength: 64 })
  @IsOptional() @IsString() @MaxLength(64)
  deviceModel?: string;

  @ApiPropertyOptional({ example: '13', maxLength: 32 })
  @IsOptional() @IsString() @MaxLength(32)
  deviceOs?: string;

  @ApiPropertyOptional({ example: '1.2.0', maxLength: 32 })
  @IsOptional() @IsString() @MaxLength(32)
  appVersion?: string;

  @ApiPropertyOptional({ example: 'zh_CN', maxLength: 16 })
  @IsOptional() @IsString() @MaxLength(16)
  locale?: string;

  @ApiPropertyOptional({ type: 'object' })
  @IsOptional() @IsObject()
  properties?: Record<string, any>;
}

export class BatchEventsDto {
  @ApiProperty({ type: [TelemetryEventItem] })
  @ValidateNested({ each: true })
  @Type(() => TelemetryEventItem)
  @ArrayMaxSize(500)
  events: TelemetryEventItem[];
}
