import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BlackListlog } from './blacklistlog.entity';
import { RpcException } from '@nestjs/microservices';

@Injectable()
export class BlacklistlogService {
  constructor(
    @InjectRepository(BlackListlog)
    private blacklistlogRepository: Repository<BlackListlog>,
  ) {}

  async getAll(): Promise<any> {
    return await this.blacklistlogRepository.find();
  }

  async getById(id: number): Promise<any> {
    const item = await this.blacklistlogRepository.findOneBy({
      idBlackList_Log: id,
    });

    return item;
  }

  async add(body: any): Promise<any> {
    const newItem = this.blacklistlogRepository.create(body);
    const saved = await this.blacklistlogRepository.save(newItem);
    return {
      message: 'Blacklist log added',
      data: saved,
    };
  }

  async update(id: number, body: any): Promise<any> {
    const item = await this.blacklistlogRepository.findOneBy({
      idBlackList_Log: id,
    });
    if (!item) {
      throw new Error('Blacklist log not found');
    }
    const updated = Object.assign(item, body);
    const saved = await this.blacklistlogRepository.save(updated);
    return {
      message: 'Blacklist log updated',
      data: saved,
    };
  }
}
