// ============================================================
// DTOs fuer Custom Fields — Feld-Definitionen erstellen
// ============================================================

import {
  IsString,
  IsEnum,
  IsOptional,
  IsBoolean,
  IsInt,
  IsArray,
  ValidateNested,
  MinLength,
  MaxLength,
  Min,
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';

// --------------------------------------------------------
// Enums (TypeScript-Pendants zu Prisma Enums)
// --------------------------------------------------------

export enum CustomFieldEntityType {
  PERSON = 'PERSON',
  COMPANY = 'COMPANY',
  DEAL = 'DEAL',
}

export enum CustomFieldType {
  TEXT = 'TEXT',
  TEXTAREA = 'TEXTAREA',
  NUMBER = 'NUMBER',
  DATE = 'DATE',
  DROPDOWN = 'DROPDOWN',
  MULTI_SELECT = 'MULTI_SELECT',
  CHECKBOX = 'CHECKBOX',
  URL = 'URL',
}

// --------------------------------------------------------
// Option-DTO (fuer DROPDOWN / MULTI_SELECT)
// --------------------------------------------------------

export class CustomFieldOptionDto {
  @ApiProperty({ description: 'Interner Wert (wird gespeichert)' })
  @IsString()
  @MinLength(1)
  value!: string;

  @ApiProperty({ description: 'Anzeigename im UI' })
  @IsString()
  @MinLength(1)
  label!: string;
}

// --------------------------------------------------------
// CreateCustomFieldDto
// --------------------------------------------------------

export class CreateCustomFieldDto {
  @ApiProperty({
    enum: CustomFieldEntityType,
    description: 'Entity-Typ, fuer den das Feld gilt',
  })
  @IsEnum(CustomFieldEntityType)
  entityType!: CustomFieldEntityType;

  @ApiProperty({
    maxLength: 200,
    description: 'Anzeigename des Feldes (daraus wird der interne Name/Slug generiert)',
  })
  @IsString()
  @MinLength(1)
  @MaxLength(200)
  label!: string;

  @ApiProperty({
    enum: CustomFieldType,
    description: 'Feldtyp (nach Erstellung nicht mehr aenderbar)',
  })
  @IsEnum(CustomFieldType)
  fieldType!: CustomFieldType;

  @ApiPropertyOptional({
    type: [CustomFieldOptionDto],
    description: 'Auswahloptionen — Pflicht fuer DROPDOWN und MULTI_SELECT',
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CustomFieldOptionDto)
  options?: CustomFieldOptionDto[];

  @ApiPropertyOptional({ default: false, description: 'Pflichtfeld?' })
  @IsOptional()
  @IsBoolean()
  isRequired?: boolean;

  @ApiPropertyOptional({ default: 0, description: 'Sortierposition im Formular' })
  @IsOptional()
  @IsInt()
  @Min(0)
  position?: number;
}
