import { PartialType } from '@nestjs/mapped-types';
import { Transform, Type } from 'class-transformer';
import {
  ArrayMaxSize,
  IsArray,
  IsInt,
  IsString,
  Matches,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator';

export class WatchlistItemDto {
  @IsString()
  @Matches(/^[a-z0-9-]{1,160}$/)
  coinId!: string;

  @Transform(({ value }) => (typeof value === 'string' ? value.trim().toUpperCase() : value))
  @IsString()
  @Matches(/^[A-Z0-9._-]{1,30}$/)
  symbol!: string;
}

export class CreateWatchlistDto {
  @IsString()
  @MaxLength(120)
  name!: string;

  @IsArray()
  @ArrayMaxSize(500)
  @ValidateNested({ each: true })
  @Type(() => WatchlistItemDto)
  items!: WatchlistItemDto[];
}

export class UpdateWatchlistDto extends PartialType(CreateWatchlistDto) {
  @IsInt()
  @Min(1)
  version!: number;
}
