import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Marchantuser } from './merchantuser.entity';
import { Repository } from 'typeorm';
import { MechantRegistrationRequestDto } from '../dto/mechant-registration.req.dto';
import * as bcrypt from 'bcrypt';

@Injectable()
export class MerchantuserService {
  constructor(
    @InjectRepository(Marchantuser)
    private marchantUserRepository: Repository<Marchantuser>,
  ) {}

  async doMerchantUserRegistration(
    userData: MechantRegistrationRequestDto,
  ): Promise<Marchantuser> {
    const trimmedPassword = userData.Password.trim();
    const trimmedConfirmPass = userData.ConfirmPass.trim();
    if (trimmedPassword !== trimmedConfirmPass) {
      throw new Error('Passwords do not match!');
    }

    const hashedPassword = await bcrypt.hash(trimmedPassword, 10);
    console.log('hash password is : ', hashedPassword);
    const user = new Marchantuser();
    user.Full_Name = userData.Full_Name;
    user.Email = userData.Email;
    user.Password = hashedPassword;
    user.Contact_no = String(userData.Contact_no);
    user.Marchant_Location_idMarchant_Location =
      userData.Marchant_Location_idMarchant_Location;

    return this.marchantUserRepository.save(user);
  }
}
