import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsInt,
  IsEnum,
  IsUUID,
  IsDateString,
  Min,
} from 'class-validator';
import { Type } from 'class-transformer';
import { BookingRecordStatus } from '@prisma/client';

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

  @ApiProperty({ example: '+79001234567' })
  @IsString()
  @IsNotEmpty()
  clientPhone: string;

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

  @ApiPropertyOptional({ description: 'Link to existing client' })
  @IsUUID()
  @IsOptional()
  clientId?: string;

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

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

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

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

  @ApiPropertyOptional({ description: 'Auto-calculated from service duration if omitted' })
  @IsDateString()
  @IsOptional()
  endAt?: string;

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

  @ApiPropertyOptional({ description: 'Source', default: 'ADMIN' })
  @IsString()
  @IsOptional()
  source?: string;

  @ApiPropertyOptional({ description: 'Pay with membership' })
  @IsUUID()
  @IsOptional()
  membershipId?: string;

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

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

export class UpdateBookingRecordDto {
  @ApiPropertyOptional()
  @IsString()
  @IsOptional()
  clientName?: string;

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

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

  @ApiPropertyOptional()
  @IsUUID()
  @IsOptional()
  staffId?: string;

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

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

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

export class UpdateRecordStatusDto {
  @ApiProperty({ enum: BookingRecordStatus })
  @IsEnum(BookingRecordStatus)
  status: BookingRecordStatus;

  @ApiPropertyOptional({ description: 'Cancel reason (if CANCELLED)' })
  @IsString()
  @IsOptional()
  cancelReason?: string;
}

export class FindRecordsQueryDto {
  @ApiPropertyOptional({ enum: BookingRecordStatus })
  @IsEnum(BookingRecordStatus)
  @IsOptional()
  status?: BookingRecordStatus;

  @ApiPropertyOptional()
  @IsUUID()
  @IsOptional()
  staffId?: string;

  @ApiPropertyOptional()
  @IsUUID()
  @IsOptional()
  serviceId?: string;

  @ApiPropertyOptional({ example: '2026-03-01' })
  @IsDateString()
  @IsOptional()
  from?: string;

  @ApiPropertyOptional({ example: '2026-03-31' })
  @IsDateString()
  @IsOptional()
  to?: string;

  @ApiPropertyOptional({ description: 'Search by client name or phone' })
  @IsString()
  @IsOptional()
  search?: string;

  @ApiPropertyOptional({ description: 'Cursor for pagination' })
  @IsUUID()
  @IsOptional()
  cursor?: string;

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