import {
  IsString,
  IsEnum,
  IsArray,
  IsOptional,
  ValidateNested,
  IsUUID,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ImportEntityType } from './import-preview.dto';

export enum DuplicateStrategy {
  SKIP = 'SKIP',
  UPDATE = 'UPDATE',
  MARK = 'MARK',
}

export class FieldMappingDto {
  @ApiProperty({ description: 'Spaltenname aus der Datei' })
  @IsString()
  sourceColumn!: string;

  @ApiProperty({ description: 'Zielfeld im CRM (z.B. firstName, email, phone)' })
  @IsString()
  targetField!: string;
}

export class ImportExecuteDto {
  @ApiProperty({ format: 'uuid', description: 'Import-ID aus der Preview' })
  @IsUUID()
  importId!: string;

  @ApiProperty({ enum: ImportEntityType })
  @IsEnum(ImportEntityType)
  entityType!: ImportEntityType;

  @ApiProperty({ type: [FieldMappingDto], description: 'Feld-Zuordnungen' })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => FieldMappingDto)
  mapping!: FieldMappingDto[];

  @ApiPropertyOptional({
    enum: DuplicateStrategy,
    default: DuplicateStrategy.SKIP,
    description: 'Strategie bei Duplikaten (E-Mail-Abgleich)',
  })
  @IsOptional()
  @IsEnum(DuplicateStrategy)
  duplicateStrategy?: DuplicateStrategy = DuplicateStrategy.SKIP;
}
