import {
  IsString,
  IsOptional,
  IsUUID,
  IsNumber,
  IsDateString,
  IsEnum,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ProjectRequestDto } from './project-request.dto';

export enum DealStatus {
  OPEN = 'OPEN',
  WON = 'WON',
  LOST = 'LOST',
}

export enum LostReason {
  PRICE = 'PRICE',
  TIMING = 'TIMING',
  COMPETITOR = 'COMPETITOR',
  NO_NEED = 'NO_NEED',
  OTHER = 'OTHER',
}

export class CreateDealDto {
  @ApiProperty({ format: 'uuid' })
  @IsUUID()
  pipelineId!: string;

  @ApiProperty({ format: 'uuid' })
  @IsUUID()
  stageId!: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  contactId?: string;

  @ApiPropertyOptional({ format: 'uuid', description: 'Zugeordnetes Unternehmen' })
  @IsOptional()
  @IsUUID()
  companyId?: string;

  @ApiProperty({ maxLength: 500 })
  @IsString()
  @MaxLength(500)
  title!: string;

  @ApiPropertyOptional({ description: 'Wert in Waehrung (z.B. 15000.00)' })
  @IsOptional()
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0)
  value?: number;

  @ApiPropertyOptional({ default: 'EUR', maxLength: 3 })
  @IsOptional()
  @IsString()
  @MaxLength(3)
  currency?: string;

  @ApiPropertyOptional({ enum: DealStatus, default: DealStatus.OPEN })
  @IsOptional()
  @IsEnum(DealStatus)
  status?: DealStatus;

  @ApiPropertyOptional({ format: 'date-time' })
  @IsOptional()
  @IsDateString()
  expectedCloseDate?: string;

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

  @ApiPropertyOptional({ enum: LostReason, description: 'Verlustgrund (Pflicht bei Status LOST)' })
  @IsOptional()
  @IsEnum(LostReason)
  lostReason?: LostReason;

  @ApiPropertyOptional({ description: 'Freitext-Begruendung zum Verlustgrund' })
  @IsOptional()
  @IsString()
  lostReasonText?: string;

  @ApiPropertyOptional({ format: 'uuid', description: 'Vorgangsart (konfigurierbar)' })
  @IsOptional()
  @IsUUID()
  dealTypeId?: string;

  @ApiPropertyOptional({
    description: 'Projektanfrage-Details (nur bei Vorgangsart mit isProjectType=true)',
    type: () => ProjectRequestDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => ProjectRequestDto)
  projectRequest?: ProjectRequestDto;
}
