import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger';
import { IsOptional, IsUUID } from 'class-validator';
import { AssetProps } from '../logic/Props';
import { UserWithNameDTO } from '../../user/dto/user-name-dto';

export class CommentBlockDTO {
  @ApiProperty({})
  ref: string;

  @IsOptional()
  @ApiProperty({
    type: 'object',
    required: false,
  })
  anchor?: AssetProps;
}

export class CommentReplyChangeDTO {
  @ApiProperty({
    type: 'object',
  })
  content: AssetProps;

  @IsUUID()
  @IsOptional()
  @ApiProperty({
    required: false,
  })
  answerToReplyId?: string;
}

export class CommentCreateDTO {
  @ApiProperty({
    type: 'object',
  })
  content: AssetProps;

  @IsUUID()
  @ApiProperty({})
  assetId: string;

  @ApiProperty({ type: CommentBlockDTO, isArray: true })
  blocks: CommentBlockDTO[];
}

export class CommentCreateResponseDTO {
  @ApiProperty({})
  replyId: string;
  @ApiProperty({})
  commentId: string;
}

export class CommentReplyDTO {
  @ApiProperty({})
  id: string;
  @ApiProperty({})
  commentId: string;
  @ApiProperty({})
  answerToId: string;
  @ApiProperty({ type: UserWithNameDTO })
  user: UserWithNameDTO;
  @ApiProperty({})
  content: AssetProps;
  @ApiProperty({})
  createdAt: string;
  @ApiProperty({})
  updatedAt: string;
}

export class GetCommentsParamsDTO {
  @IsOptional()
  @ApiProperty({
    required: false,
  })
  offset?: string;

  @IsOptional()
  @ApiProperty({
    required: false,
  })
  count?: string;
}

@ApiExtraModels(CommentReplyDTO)
export class GetCommentsResultObjectDTO {
  @ApiProperty({
    type: 'object',
    additionalProperties: { $ref: getSchemaPath(CommentReplyDTO) },
  })
  replies: {
    [key: string]: CommentReplyDTO;
  };
}

export class GetCommentsResultDTO {
  @ApiProperty()
  ids: string[];

  @ApiProperty({ type: GetCommentsResultObjectDTO })
  objects: GetCommentsResultObjectDTO;

  @ApiProperty()
  more: boolean;
}
