import { BaseRepository } from '@edr/api-common';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DeepPartial, EntityManager, In, Repository } from 'typeorm';

import { WagonAllocationBulkLoad } from './entities/wagon-allocation-bulk-load.entity';

@Injectable()
export class WagonAllocationBulkLoadsRepository extends BaseRepository<WagonAllocationBulkLoad> {
  constructor(
    @InjectRepository(WagonAllocationBulkLoad)
    repository: Repository<WagonAllocationBulkLoad>,
  ) {
    super(repository);
  }

  private repo(manager?: EntityManager) {
    return manager
      ? manager.getRepository(WagonAllocationBulkLoad)
      : this.repository;
  }

  async createMany(
    items: DeepPartial<WagonAllocationBulkLoad>[],
    manager?: EntityManager,
  ): Promise<WagonAllocationBulkLoad[]> {
    if (!items.length) return [];
    const repo = this.repo(manager);
    return repo.save(repo.create(items));
  }

  async deleteByAllocationIds(allocationIds: string[], manager?: EntityManager): Promise<void> {
    if (!allocationIds.length) return;
    await this.repo(manager).delete({ wagonBookingAllocationId: In(allocationIds) });
  }
}
