import { Injectable } from '@nestjs/common';

import { InjectRepository } from '@nestjs/typeorm';
// import { UsersEntity } from 'src/entities/Users.entity';
import { Repository } from 'typeorm';
import { EventEmitter2 } from '@nestjs/event-emitter';
// import { PasswordResetsEntity } from 'src/entities/PasswordResets.entity';
import { OneHourAgo, generateRandomString } from 'src/utils';
import { ResetPassEvent } from '../events/resetpass-event';
import { encryptPassword } from '../../../utils/encryption';

@Injectable()
export class ResetPasswordService {
  // constructor(
  //   @InjectRepository(UsersEntity)
  //   private readonly userRepository: Repository<UsersEntity>,
  //   private eventEmitter: EventEmitter2,
  //   @InjectRepository(PasswordResetsEntity)
  //   private readonly resendTokenRepository: Repository<PasswordResetsEntity>,
  // ) {}
  // // Send Resend Password Link
  // async sendLink(req): Promise<void> {
  //   const user = await this.userRepository.findOne({
  //     where: { email: req.email },
  //   });
  //   if (!user) {
  //     throw {
  //       success: false,
  //       errors: { email: 'Please enter a registered email address.' },
  //       data: null,
  //     };
  //   }
  //   const token = generateRandomString(50);
  //   const { flag } = req;
  //   const resetToken = new PasswordResetsEntity();
  //   resetToken.email = req.email;
  //   resetToken.flag = req.flag;
  //   resetToken.token = token;
  //   resetToken.created_at = new Date();
  //   await this.resendTokenRepository.insert(resetToken);
  //   this.eventEmitter.emit(
  //     'user.resetpass',
  //     new ResetPassEvent(user, token, flag),
  //   );
  // }
  // /**
  //  * Reset Password Service
  //  */
  // async reset(password: string, token: string): Promise<void> {
  //   const usertoken = await this.resendTokenRepository.findOne({
  //     where: {
  //       token,
  //     },
  //   });
  //   if (!usertoken) {
  //     throw {
  //       success: false,
  //       errors: { token: 'Token not found.' },
  //       data: null,
  //     };
  //   }
  //   if (OneHourAgo(usertoken.created_at) === false) {
  //     throw {
  //       success: false,
  //       errors: { token: 'Your password reset token has expired!' },
  //       data: null,
  //     };
  //   }
  //   const user = await this.userRepository.findOneOrFail({
  //     where: { email: usertoken.email },
  //   });
  //   user.password = await encryptPassword(password);
  //   this.userRepository.save(user);
  // }
}
