import {
  ArrayUnique,
  IsArray,
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  Matches,
  Max,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator'
import { Type } from 'class-transformer'

class FlagScopeDto {
  @IsOptional() @IsArray() @ArrayUnique() @IsString({ each: true })
  plans?: string[]

  @IsOptional() @IsArray() @ArrayUnique() @IsString({ each: true })
  tenantSlugs?: string[]

  @IsOptional() @IsArray() @ArrayUnique() @IsString({ each: true })
  partnerSlugs?: string[]

  @IsOptional() @IsArray() @ArrayUnique()
  @IsEnum(['prod', 'staging', 'dev'], { each: true })
  environments?: ('prod' | 'staging' | 'dev')[]
}

export class CreateFlagDto {
  @IsString()
  @Matches(/^[a-z][a-z0-9_]{1,62}[a-z0-9]$/, {
    message: 'key must be lowercase snake_case, 3-64 chars, starts with a letter',
  })
  key!: string

  @IsOptional() @IsString() @MaxLength(280)
  description?: string

  @IsOptional() @IsEnum(['off', 'targeted', 'rollout', 'on'])
  state?: 'off' | 'targeted' | 'rollout' | 'on'

  @IsOptional() @IsInt() @Min(0) @Max(100)
  pct?: number

  @IsOptional() @ValidateNested() @Type(() => FlagScopeDto)
  scope?: FlagScopeDto
}
