import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsString, Matches, MaxLength, MinLength } from 'class-validator';

// Trim incoming names so " In Progress " and "In Progress" are treated
// the same at validation time (uniqueness checks happen later in the service).
const trimString = ({ value }: { value: unknown }) =>
  typeof value === 'string' ? value.trim() : value;

// Trim AND upper-case the code so callers can send "in_progress" and we
// still store the canonical UPPER_SNAKE form. Format validation runs after.
const toUpperSnake = ({ value }: { value: unknown }) =>
  typeof value === 'string' ? value.trim().toUpperCase() : value;

export class CreateIssueStatusDto {
  @ApiProperty({
    example: 'In Progress',
    description: 'Human-readable status name. Must be unique.',
  })
  @Transform(trimString)
  @IsString()
  @MinLength(1)
  @MaxLength(100)
  name!: string;

  @ApiProperty({
    example: 'IN_PROGRESS',
    description:
      'Stable machine identifier. UPPER_SNAKE_CASE (A–Z, 0–9, underscore). Must be unique.',
  })
  @Transform(toUpperSnake)
  @IsString()
  @MinLength(1)
  @MaxLength(50)
  @Matches(/^[A-Z][A-Z0-9_]*$/, {
    message:
      'code must be UPPER_SNAKE_CASE (start with a letter, A–Z, 0–9, underscore only)',
  })
  code!: string;
}
