import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CustomerSavingLog } from './customersavinglog.entity';

@Injectable()
export class CustomersavinglogService {
  constructor(
    @InjectRepository(CustomerSavingLog)
    private readonly logRepo: Repository<CustomerSavingLog>,
  ) {}

  async getAll(): Promise<CustomerSavingLog[]> {
    return await this.logRepo.find();
  }

  async getById(id: number): Promise<CustomerSavingLog | null> {
    return await this.logRepo.findOneBy({ idCustomer_Saving_Log: id });
  }

  async getBySavingId(id: number): Promise<CustomerSavingLog[]> {
    return await this.logRepo.find({
      where: { Customer_Savings_idCustomer_Savings: id },
    });
  }

  async add(data: any): Promise<any> {
    const log = this.logRepo.create(data);
    await this.logRepo.save(log);
    return { success: true, message: 'Saving log added successfully' };
  }
}
