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

const VALID_VITAL_NAMES = ['LCP', 'FCP', 'CLS', 'TTFB', 'INP'] as const;
const VALID_RATINGS = ['good', 'needs-improvement', 'poor'] as const;

export class WebVitalMetricDto {
  @ApiProperty({ enum: VALID_VITAL_NAMES, description: 'Core Web Vital name' })
  @IsIn(VALID_VITAL_NAMES)
  name!: string;

  @ApiProperty({ description: 'Metric value (ms for timing metrics, unitless for CLS)' })
  @IsNumber()
  @Min(0)
  value!: number;

  @ApiProperty({ enum: VALID_RATINGS, description: 'Performance rating' })
  @IsIn(VALID_RATINGS)
  rating!: string;

  @ApiProperty({ description: 'Delta since last report' })
  @IsNumber()
  delta!: number;

  @ApiProperty({ description: 'Unique metric ID from web-vitals' })
  @IsString()
  @MaxLength(128)
  id!: string;

  @ApiProperty({ description: 'Navigation type (navigate, reload, etc.)' })
  @IsString()
  @MaxLength(64)
  navigationType!: string;

  @ApiProperty({ description: 'Page URL path' })
  @IsString()
  @MaxLength(2048)
  url!: string;

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

export class WebVitalsBatchDto {
  @ApiProperty({ type: [WebVitalMetricDto], description: 'Batch of web vital metrics' })
  @IsArray()
  @ArrayMaxSize(50)
  @ValidateNested({ each: true })
  @Type(() => WebVitalMetricDto)
  metrics!: WebVitalMetricDto[];
}
