import { ApiProperty } from "@nestjs/swagger";
import { IsNotEmpty, IsArray  } from "class-validator";
import { extname } from "path";

export class CreateAttachmentDto {
    @IsNotEmpty()
    @IsArray()
    @ApiProperty()
    name: string;
  
    @IsNotEmpty()
    @ApiProperty()
    attachment_type: string;


    // @IsNotEmpty()
    // @ApiProperty()
    // activity_id: number;

    // @IsNotEmpty()
    // @ApiProperty()
    // tokenId: string;

    @ApiProperty()
    user_id: number;

    @ApiProperty()
    dateCreated: Date;

}


export class UpdateAttachmentDto {
    @IsNotEmpty()
    @ApiProperty()
    name: string;
  
    @IsNotEmpty()
    @ApiProperty()
    attachment_type: string;

    @ApiProperty()
    user_id: number;

    @ApiProperty()
    dateUpdated: Date;
}

export const editFileName = (req, file, callback) => {
  const name = file.originalname.split('.')[0];
  const curren_data_time = new Date().getTime()
  const fileExtName = extname(file.originalname);
  const randomName = Array(4)
    .fill(null)
    .map(() => curren_data_time+Math.round(Math.random() * 4).toString(5))
    .join('');
  callback(null, `${randomName}${fileExtName}`);
};

export const imageFileFilter = (req, file, callback) => {
  if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
    return callback(new Error('Only image files are allowed!'), false);
  }
  callback(null, true);
};  

