import { Inject, Injectable, forwardRef } from '@nestjs/common'
import { FindManyOptions, FindOptionsOrder, In, Repository, UpdateResult } from 'typeorm'
import { InjectRepository } from '@nestjs/typeorm'
import { paginate, Pagination } from 'nestjs-typeorm-paginate'
import { PageRequest } from '../common/dto/page-request'
import { Condition } from './entities/condition.entity'
import { Moments } from 'src/moments/entities/moments.entity'
import { Comment } from 'src/comment/entities/comment.entity'
import { ChatService } from 'src/chat/chat.service'
import { CommentDto } from 'src/comment/dto/comment.dto'
import { CommentService } from 'src/comment/comment.service'
import { ConditionDto } from './dto/condition.dto'

@Injectable()
export class ConditionService {
    constructor(
        @InjectRepository(Condition)
        private readonly conditionRepository: Repository<Condition>,
        @InjectRepository(Moments)
        private readonly momentsRepository: Repository<Moments>,
        @InjectRepository(Comment)
        private readonly commentRepository: Repository<Comment>,
        private readonly chatService: ChatService,
        @Inject(forwardRef(() => CommentService))
        private readonly commentService: CommentService
    ) {}

    public async create() {}

    public async reply(comment: Comment, moments: Moments) {
        const chat = await this.chatService.sendMessage(null, moments.messageId, comment.content)

        if (chat.message.trim() !== '') {
            const favorabilityChat = await this.chatService.sendMessage(
                null,
                chat.messageId,
                '这是我的评论<' +
                    comment.content +
                    '>,请你就这条评论打分,分数在0到10这个区间,返回数字,不必做解释,不要出现文字'
            )
            const fraction = favorabilityChat.message

            const dto = new ConditionDto()
            dto.userId = comment.userId
            dto.favorability = parseFloat(fraction)
            dto.chatRoleId = moments.userId

            const commentDto = new CommentDto()
            commentDto.content = chat.message
            commentDto.userId = comment.userId
            commentDto.userName = comment.userName
            commentDto.momentsId = comment.momentsId
            commentDto.rootCommentId = comment.rootCommentId
            commentDto.toCommentId = comment.id

            const replyComment = await this.commentService.create(commentDto)
            await this.conditionRepository.save(dto)
        }
    }
}
