import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsBoolean, IsOptional, IsString, IsArray, IsNotEmpty, IsUrl } from 'class-validator';
import { UserEntity } from '../../user/entity/users.entity';
import { Transform } from 'class-transformer';

export class CreateLibraryDto {
  @ApiProperty({ type: 'string', format: 'binary' })
  file: Express.Multer.File;

  @ApiPropertyOptional()
  @IsOptional()
  attachmentUrl?: string;

  @ApiPropertyOptional()
  @IsOptional()
  //   @IsNotEmpty()
  @IsString()
  attachmentName?: string;

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

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

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

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  attachmentCategory: string;

  @ApiPropertyOptional()
  @IsOptional()
  attachmentPostedBy?: UserEntity;

  @ApiPropertyOptional({ default: false })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => {
    if (value === 'true') return true;
    if (value === 'false') return false;
    return value;
  })
  isPublic?: boolean;

  @ApiPropertyOptional({ type: [String] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  @Transform(({ value }) => {
    if (typeof value === 'string') return [value];
    return value;
  })
  tags?: string[];

  @ApiPropertyOptional({ default: true })
  @IsOptional()
  @IsBoolean()
  @Transform(({ value }) => {
    if (value === 'true') return true;
    if (value === 'false') return false;
    return value;
  })
  isActive?: boolean;
}
