import {
  IsString,
  IsOptional,
  IsBoolean,
  IsEmail,
  IsUrl,
  IsUUID,
  IsArray,
  IsEnum,
  MaxLength,
  ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { CreateEmailDto, CreatePhoneDto, EntityStatus, CompanySize } from '../../common/dto/contact-info.dto';

export class CreateCompanyDto {
  @ApiProperty({ maxLength: 200 })
  @IsString()
  @MaxLength(200)
  name!: string;

  @ApiPropertyOptional({ maxLength: 100, description: 'Freitext-Branche (Legacy)' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  industry?: string;

  @ApiPropertyOptional({ format: 'uuid', description: 'Branchen-ID' })
  @IsOptional()
  @IsUUID()
  industryId?: string;

  @ApiPropertyOptional({ format: 'uuid', description: 'Kontotyp-ID' })
  @IsOptional()
  @IsUUID()
  accountTypeId?: string;

  @ApiPropertyOptional({ format: 'uuid', description: 'Zustaendiger User (Owner-ID)' })
  @IsOptional()
  @IsUUID()
  ownerId?: string;

  @ApiPropertyOptional({ maxLength: 200, description: 'Name des zustaendigen Users' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  ownerName?: string;

  @ApiPropertyOptional({ maxLength: 50, description: 'USt-IdNr.' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  vatId?: string;

  @ApiPropertyOptional({ maxLength: 50, description: 'Steuernummer' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  taxId?: string;

  @ApiPropertyOptional({ maxLength: 100, description: 'Handelsregisternummer (z.B. HRB 12345)' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  tradeRegisterNumber?: string;

  @ApiPropertyOptional({ maxLength: 200, description: 'Registergericht' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  registerCourt?: string;

  @ApiPropertyOptional({ enum: CompanySize, description: 'Unternehmensgroesse' })
  @IsOptional()
  @IsEnum(CompanySize)
  companySize?: CompanySize;

  @ApiPropertyOptional({ maxLength: 255 })
  @IsOptional()
  @IsEmail()
  @MaxLength(255)
  email?: string;

  @ApiPropertyOptional({ maxLength: 50 })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  phone?: string;

  @ApiPropertyOptional({ maxLength: 500 })
  @IsOptional()
  @IsUrl()
  @MaxLength(500)
  website?: string;

  @ApiPropertyOptional({ maxLength: 200 })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  street?: string;

  @ApiPropertyOptional({ maxLength: 20 })
  @IsOptional()
  @IsString()
  @MaxLength(20)
  zip?: string;

  @ApiPropertyOptional({ maxLength: 100 })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  city?: string;

  @ApiPropertyOptional({ maxLength: 100 })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  state?: string;

  @ApiPropertyOptional({ maxLength: 2, default: 'DE' })
  @IsOptional()
  @IsString()
  @MaxLength(2)
  country?: string;

  @ApiPropertyOptional({ maxLength: 200, description: 'Lieferadresse: Strasse' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  deliveryStreet?: string;

  @ApiPropertyOptional({ maxLength: 20, description: 'Lieferadresse: PLZ' })
  @IsOptional()
  @IsString()
  @MaxLength(20)
  deliveryZip?: string;

  @ApiPropertyOptional({ maxLength: 100, description: 'Lieferadresse: Stadt' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  deliveryCity?: string;

  @ApiPropertyOptional({ maxLength: 2, description: 'Lieferadresse: Land (ISO 2-Zeichen)' })
  @IsOptional()
  @IsString()
  @MaxLength(2)
  deliveryCountry?: string;

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

  @ApiPropertyOptional({ type: [String] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  tags?: string[];

  @ApiPropertyOptional({ enum: EntityStatus, default: EntityStatus.ACTIVE, description: 'Status (ersetzt isActive)' })
  @IsOptional()
  @IsEnum(EntityStatus)
  status?: EntityStatus;

  @ApiPropertyOptional({ default: true, deprecated: true, description: 'DEPRECATED: Bitte status verwenden' })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;

  @ApiPropertyOptional({ type: [CreateEmailDto], description: 'E-Mail-Adressen (Multi-Value)' })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateEmailDto)
  emails?: CreateEmailDto[];

  @ApiPropertyOptional({ type: [CreatePhoneDto], description: 'Telefonnummern (Multi-Value)' })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreatePhoneDto)
  phones?: CreatePhoneDto[];
}
