import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ListingStatus } from '@prisma/client';
import { Transform, Type } from 'class-transformer';
import {
  ArrayMaxSize,
  ArrayMinSize,
  ArrayUnique,
  IsArray,
  IsBoolean,
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  IsUUID,
  Max,
  Min,
  ValidateNested,
} from 'class-validator';
import { BULK_UPDATE_MAX_ITEMS } from '../../application/commands/bulk-update-listings/bulk-update-listings.handler';

export class BulkUpdatePatchDto {
  @ApiPropertyOptional({ type: String, example: '5500000000', description: 'New price in VND (string for bigint)' })
  @IsOptional()
  @Transform(({ value }) => (value !== undefined && value !== null && value !== '' ? BigInt(value) : undefined))
  priceVND?: bigint;

  @ApiPropertyOptional({ enum: ListingStatus, example: 'ACTIVE' })
  @IsOptional()
  @IsEnum(ListingStatus)
  status?: ListingStatus;

  @ApiPropertyOptional({ example: true, description: 'true → mark featured for `featuredDays`; false → clear featured' })
  @IsOptional()
  @IsBoolean()
  featured?: boolean;

  @ApiPropertyOptional({ example: 7, description: 'Required when featured=true' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(365)
  featuredDays?: number;
}

export class BulkUpdateListingsDto {
  @ApiProperty({
    type: [String],
    description: `Listing IDs to update (max ${BULK_UPDATE_MAX_ITEMS}, must be unique)`,
    example: ['a1b2c3d4-e5f6-7890-abcd-ef1234567890'],
  })
  @IsArray()
  @ArrayMinSize(1)
  @ArrayMaxSize(BULK_UPDATE_MAX_ITEMS)
  @ArrayUnique()
  @IsString({ each: true })
  @IsUUID('all', { each: true })
  ids!: string[];

  @ApiProperty({ type: BulkUpdatePatchDto, description: 'Patch to apply to every listing' })
  @ValidateNested()
  @Type(() => BulkUpdatePatchDto)
  patch!: BulkUpdatePatchDto;
}
