import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsEnum,
  IsBoolean,
  IsArray,
  Matches,
} from 'class-validator';
import { CrmUserFieldType } from '@prisma/client';

export class CreateFieldDto {
  @ApiProperty({ description: 'Entity type (e.g., "deal", "training")' })
  @IsString()
  @IsNotEmpty()
  entityType: string;

  @ApiProperty({ description: 'Display name (RU)', example: 'Размер обуви' })
  @IsString()
  @IsNotEmpty()
  name: string;

  @ApiProperty({
    description: 'Field name in DB (EN, snake_case)',
    example: 'shoe_size',
  })
  @IsString()
  @IsNotEmpty()
  @Matches(/^[a-z][a-z0-9_]*$/, {
    message: 'fieldName must be lowercase English with underscores',
  })
  fieldName: string;

  @ApiProperty({ enum: CrmUserFieldType, default: 'STRING' })
  @IsEnum(CrmUserFieldType)
  type: CrmUserFieldType;

  @ApiPropertyOptional({
    description: 'Options for LIST type',
    example: ['S', 'M', 'L', 'XL'],
  })
  @IsArray()
  @IsOptional()
  listOptions?: string[];

  @ApiPropertyOptional({ default: false })
  @IsBoolean()
  @IsOptional()
  isRequired?: boolean;

  @ApiPropertyOptional({ default: false })
  @IsBoolean()
  @IsOptional()
  isMultiple?: boolean;

  @ApiPropertyOptional({ default: false })
  @IsBoolean()
  @IsOptional()
  showToTrainer?: boolean;

  @ApiPropertyOptional({
    description: 'Per-role access: { "trainer": "hidden"|"readonly"|"editable", ... }',
    example: { trainer: 'readonly', manager: 'editable', club_admin: 'editable' },
  })
  @IsOptional()
  roleAccess?: Record<string, string>;

  @ApiPropertyOptional()
  @IsString()
  @IsOptional()
  defaultValue?: string;

  @ApiPropertyOptional()
  @IsString()
  @IsOptional()
  description?: string;
}
