import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsString, IsOptional, IsObject, IsNumber, ValidateNested } from 'class-validator';
import { ClientDeviceDto } from './client-device.dto';
import { AttributionDto } from './attribution.dto';

/**
 * Page view tracking DTO
 *
 * Accepts both the collector's original shape (pageUrl) and the
 * @lilith/analytics-client ViewEventData shape (contentId + contentType).
 * The controller normalises both into the same TrackViewInput.
 */
export class TrackViewDto {
  @ApiProperty({ description: 'Full URL of the page being viewed', required: false })
  @IsOptional()
  @IsString()
  pageUrl?: string;

  @ApiProperty({ description: 'Content identifier (from analytics-client)', required: false })
  @IsOptional()
  @IsString()
  contentId?: string;

  @ApiProperty({ description: 'Content type (page, post, video, etc.)', required: false })
  @IsOptional()
  @IsString()
  contentType?: string;

  @ApiProperty({ description: 'Application name (from analytics-client)', required: false })
  @IsOptional()
  @IsString()
  app?: string;

  @ApiProperty({ description: 'View duration in milliseconds', required: false })
  @IsOptional()
  @IsNumber()
  duration?: number;

  @ApiProperty({ description: 'Device type (mobile, tablet, desktop)', required: false })
  @IsOptional()
  @IsString()
  deviceType?: string;

  @ApiProperty({ description: 'Referrer URL', required: false })
  @IsOptional()
  @IsString()
  referrer?: string;

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

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

  @ApiProperty({ description: 'Original domain the visitor used to reach the site', required: false })
  @IsOptional()
  @IsString()
  domain?: string;

  @ApiProperty({ description: 'Additional metadata', required: false })
  @IsOptional()
  @IsObject()
  metadata?: Record<string, unknown>;

  @ApiProperty({
    description: 'Client-side device data from navigator APIs',
    type: ClientDeviceDto,
    required: false,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => ClientDeviceDto)
  clientDevice?: ClientDeviceDto;

  @ApiProperty({
    description: 'UTM attribution parameters (first-touch)',
    type: AttributionDto,
    required: false,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => AttributionDto)
  attribution?: AttributionDto;
}
