import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsUUID,
  IsNumber,
  IsEnum,
  IsInt,
  IsDateString,
  Min,
  Max,
  IsObject,
} from 'class-validator';
import { CrmDealSource } from '@prisma/client';

export class CreateDealDto {
  @ApiProperty({ description: 'Deal title', example: 'Новый клуб FitLife' })
  @IsString()
  @IsNotEmpty()
  title: string;

  @ApiProperty({ description: 'Contact name', example: 'Иванов Иван' })
  @IsString()
  @IsNotEmpty()
  contactName: string;

  @ApiPropertyOptional({ description: 'Pipeline ID (uses default if empty)' })
  @IsUUID()
  @IsOptional()
  pipelineId?: string;

  @ApiPropertyOptional({ description: 'Stage ID (uses default if empty)' })
  @IsUUID()
  @IsOptional()
  stageId?: string;

  @ApiPropertyOptional({ description: 'Assignee user ID' })
  @IsUUID()
  @IsOptional()
  assigneeId?: string;

  @ApiPropertyOptional({ description: 'Deal amount', example: 50000 })
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0)
  @IsOptional()
  amount?: number;

  // Contact
  @ApiPropertyOptional({ example: '+79991234567' })
  @IsString()
  @IsOptional()
  contactPhone?: string;

  @ApiPropertyOptional({ example: 'client@mail.ru' })
  @IsString()
  @IsOptional()
  contactEmail?: string;

  @ApiPropertyOptional({ example: '@username' })
  @IsString()
  @IsOptional()
  contactTelegram?: string;

  @ApiPropertyOptional({ example: '+79991234567' })
  @IsString()
  @IsOptional()
  contactWhatsapp?: string;

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

  // Company requisites
  @ApiPropertyOptional()
  @IsString()
  @IsOptional()
  companyName?: string;

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

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

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

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

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

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

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

  // Meta
  @ApiPropertyOptional({ enum: CrmDealSource, default: 'MANUAL' })
  @IsEnum(CrmDealSource)
  @IsOptional()
  source?: CrmDealSource;

  @ApiPropertyOptional({ description: 'Probability 0-100' })
  @IsInt()
  @Min(0)
  @Max(100)
  @IsOptional()
  probability?: number;

  @ApiPropertyOptional()
  @IsDateString()
  @IsOptional()
  expectedCloseDate?: string;

  @ApiPropertyOptional({ description: 'Arbitrary metadata (UTM, analytics)' })
  @IsObject()
  @IsOptional()
  metadata?: Record<string, any>;

  @ApiPropertyOptional({ description: 'Custom field values (fieldName → value)' })
  @IsObject()
  @IsOptional()
  customFieldValues?: Record<string, any>;
}
