import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  IsString,
  IsOptional,
  IsObject,
  IsArray,
  IsNumber,
  ValidateNested,
  IsIn,
} from 'class-validator';

/**
 * Single interaction event payload.
 * Matches InteractionEventPayload from @lilith/domain-events.
 */
export class InteractionEventDto {
  @ApiProperty({
    description: 'Interaction type',
    enum: ['click', 'scroll', 'funnel_step', 'resize'],
    example: 'click',
  })
  @IsString()
  @IsIn(['click', 'scroll', 'funnel_step', 'resize'])
  type!: 'click' | 'scroll' | 'funnel_step' | 'resize';

  @ApiProperty({
    description: 'Event-specific data (ClickEventData, ScrollEventData, FunnelStepData, or ResizeEventData)',
  })
  @IsObject()
  data!: Record<string, unknown>;

  @ApiProperty({ description: 'Session ID from client' })
  @IsString()
  sessionId!: string;

  @ApiProperty({ description: 'User ID if authenticated', required: false })
  @IsOptional()
  @IsString()
  userId?: string;

  @ApiProperty({ description: 'Client-side timestamp (Unix epoch ms)' })
  @IsNumber()
  timestamp!: number;
}

/**
 * Batch of interaction events DTO.
 * Matches the payload sent by @lilith/analytics-client flushInteractions().
 */
export class TrackInteractionBatchDto {
  @ApiProperty({
    description: 'Batch of interaction events',
    type: [InteractionEventDto],
  })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => InteractionEventDto)
  events!: InteractionEventDto[];
}
