import {
  IsDateString,
  IsEmail,
  IsInt,
  IsOptional,
  IsString,
  IsUUID,
  Matches,
  Min,
  MinLength,
  ValidateIf,
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';

export class PublicBookDto {
  @ApiProperty({ example: 'Иван Петров' })
  @IsString()
  @MinLength(2)
  clientName: string;

  @ApiProperty({ example: '+79001234567' })
  @IsString()
  @Matches(/^\+?[0-9]{10,15}$/, { message: 'Invalid phone format' })
  clientPhone: string;

  @ApiPropertyOptional({ example: 'ivan@mail.ru' })
  @IsOptional()
  @IsEmail()
  clientEmail?: string;

  @ApiProperty()
  @IsUUID()
  serviceId: string;

  @ApiProperty()
  @IsUUID()
  staffId: string;

  @ApiPropertyOptional({ description: 'For group event booking' })
  @IsOptional()
  @IsUUID()
  groupEventId?: string;

  @ApiProperty({ example: '2026-03-15T10:00:00Z' })
  @IsDateString()
  startAt: string;

  @ApiPropertyOptional({ default: 1 })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Type(() => Number)
  places?: number;

  @ApiPropertyOptional({ description: 'Comment from client' })
  @IsOptional()
  @IsString()
  comment?: string;

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

  /**
   * Honeypot field — must be empty. Bots fill this automatically.
   * Named innocently to trick bot auto-fillers.
   */
  @ApiPropertyOptional({ description: 'Do not fill (anti-spam)' })
  @IsOptional()
  @IsString()
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  @ValidateIf((o) => o.website !== undefined && o.website !== '')
  website?: string;
}

export class PublicVerifyPhoneDto {
  @ApiProperty({ example: '+79001234567' })
  @IsString()
  @Matches(/^\+?[0-9]{10,15}$/)
  phone: string;
}

export class PublicVerifyCodeDto {
  @ApiProperty({ example: '+79001234567' })
  @IsString()
  @Matches(/^\+?[0-9]{10,15}$/)
  phone: string;

  @ApiProperty({ example: '1234' })
  @IsString()
  @Matches(/^[0-9]{4,6}$/)
  code: string;
}
