import {
  IsBoolean,
  IsOptional,
  IsString,
  MaxLength,
  Matches,
  ValidateIf,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class UpdateUserDto {
  @ApiProperty({ example: 'Max', required: false })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  firstName?: string;

  @ApiProperty({ example: 'Mustermann', required: false })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  lastName?: string;

  @ApiProperty({ example: true, required: false })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;

  @ApiProperty({
    description: 'Profilbild als Base64 Data-URL (max. 400KB), null zum Entfernen',
    example: 'data:image/png;base64,iVBORw0KGgo...',
    required: false,
    nullable: true,
  })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.avatar !== null)
  @IsString()
  @MaxLength(400000, { message: 'Profilbild darf maximal 400KB groß sein' })
  @Matches(/^data:image\/(jpeg|png|gif|webp);base64,[A-Za-z0-9+/=]+$/, {
    message: 'Profilbild muss ein gültiges Base64-Bild sein (JPEG, PNG, GIF oder WebP)',
  })
  avatar?: string | null;

  // --- Kontaktdaten ---
  @ApiProperty({ example: '+49 123 456789', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.phone !== null)
  @IsString()
  @MaxLength(30)
  phone?: string | null;

  @ApiProperty({ example: '+49 170 1234567', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.mobile !== null)
  @IsString()
  @MaxLength(30)
  mobile?: string | null;

  // --- Adresse ---
  @ApiProperty({ example: 'Musterstraße 42', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.street !== null)
  @IsString()
  @MaxLength(200)
  street?: string | null;

  @ApiProperty({ example: '12345', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.postalCode !== null)
  @IsString()
  @MaxLength(10)
  postalCode?: string | null;

  @ApiProperty({ example: 'Berlin', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.city !== null)
  @IsString()
  @MaxLength(100)
  city?: string | null;

  // --- Organisation (analog Microsoft 365 /me) ---
  @ApiProperty({ example: 'Senior Developer', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.jobTitle !== null)
  @IsString()
  @MaxLength(100)
  jobTitle?: string | null;

  @ApiProperty({ example: 'Engineering', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.department !== null)
  @IsString()
  @MaxLength(100)
  department?: string | null;

  @ApiProperty({ example: 'Acme GmbH', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.companyName !== null)
  @IsString()
  @MaxLength(200)
  companyName?: string | null;

  @ApiProperty({ example: 'Berlin Office', required: false, nullable: true })
  @IsOptional()
  @ValidateIf((o: UpdateUserDto) => o.officeLocation !== null)
  @IsString()
  @MaxLength(200)
  officeLocation?: string | null;
}
