import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { TransferCategory } from '@prisma/client';
import { Type } from 'class-transformer';
import {
  ArrayMaxSize,
  IsArray,
  IsEnum,
  IsInt,
  IsNumber,
  IsOptional,
  IsString,
  IsUrl,
  ValidateNested,
} from 'class-validator';

export class ImageBase64Dto {
  @ApiProperty({ description: 'Base64-encoded image data' })
  @IsString()
  data!: string;

  @ApiProperty({ enum: ['image/jpeg', 'image/png', 'image/webp', 'image/gif'] })
  @IsString()
  mediaType!: 'image/jpeg' | 'image/png' | 'image/webp' | 'image/gif';
}

export class PhotoEstimateItemDto {
  @ApiPropertyOptional({ description: 'Image URLs to assess', type: [String] })
  @IsOptional()
  @IsArray()
  @IsUrl({}, { each: true, message: 'URL ảnh không hợp lệ' })
  @ArrayMaxSize(4, { message: 'Tối đa 4 ảnh mỗi item' })
  imageUrls?: string[];

  @ApiPropertyOptional({ description: 'Base64-encoded images', type: [ImageBase64Dto] })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ImageBase64Dto)
  @ArrayMaxSize(4, { message: 'Tối đa 4 ảnh mỗi item' })
  imageBase64?: ImageBase64Dto[];

  @ApiPropertyOptional({ enum: TransferCategory, description: 'Danh mục (nếu biết)' })
  @IsOptional()
  @IsEnum(TransferCategory)
  category?: TransferCategory;

  @ApiPropertyOptional({ description: 'Giá mua ban đầu (VND)' })
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  originalPriceVND?: number;

  @ApiPropertyOptional({ description: 'Năm mua' })
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  purchaseYear?: number;

  @ApiPropertyOptional({ description: 'Thương hiệu (nếu biết)' })
  @IsOptional()
  @IsString()
  brand?: string;
}

export class EstimateFromPhotosDto {
  @ApiProperty({ type: [PhotoEstimateItemDto], description: 'Items to assess from photos' })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => PhotoEstimateItemDto)
  @ArrayMaxSize(5, { message: 'Tối đa 5 items mỗi lần đánh giá' })
  items!: PhotoEstimateItemDto[];
}
