import {
  ArrayUnique,
  IsArray,
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  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')[]
}

// `key` is immutable after create — rename would silently invalidate every
// flag check baked into consumer code.
export class UpdateFlagDto {
  @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

  // Optional free-form note attached to the resulting history entry.
  @IsOptional() @IsString() @MaxLength(280)
  note?: string
}
