import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CustomerVerifications } from './customerverification.entity';
import { Repository } from 'typeorm';

@Injectable()
export class CustomerverificationsService {
  constructor(
    @InjectRepository(CustomerVerifications)
    private customerVerifyRepo: Repository<CustomerVerifications>,
  ) {}

  async addCustomerVerification(
    Customer_idCustomer: number,
    Date_Time: string,
    Evidence_Type: string,
    Status: string,
    Note: string,
    Verification_Type: string,
    User_idUser: number,
    Document: string,
  ): Promise<any> {
    const obj = await this.customerVerifyRepo.create({
      Customer_idCustomer,
      Date_Time,
      Evidence_Type,
      Status,
      Note,
      Verification_Type,
      User_idUser,
      Document,
    });

    await this.customerVerifyRepo.save(obj);
    return { success: true, message: 'added the customer verification log' };
  }

  async getAll(): Promise<any> {
    const data = await this.customerVerifyRepo.query(
      'SELECT cv.*,c.First_Name AS customerFirstName,c.Last_Name AS customerLastName,u.Full_Name AS userName FROM customer_verifications cv INNER JOIN customer c ON cv.Customer_idCustomer = c.idCustomer INNER JOIN user u ON cv.User_idUser = u.idUser ORDER BY cv.idCustomer_Verifications DESC',
    );
    return data;
  }

  async getVerificationByCustomerId(id: number): Promise<any> {
    const data = await this.customerVerifyRepo.findBy({
      Customer_idCustomer: id,
    });
    return data;
  }
}
