import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Player } from 'src/players/entities/player.entity';
import { TransactionType } from 'src/transactions/entitites/transaction-type.entity';
import { Transaction } from 'src/transactions/entitites/transaction.entity';
import { Repository } from 'typeorm';
import { faker } from '@faker-js/faker';

@Injectable()
export class TransactionsSeedService {
  constructor(
    @InjectRepository(Transaction)
    private repository: Repository<Transaction>,
    @InjectRepository(Player)
    private playerRepository: Repository<Player>,
    @InjectRepository(TransactionType)
    private transactionTypeRepository: Repository<TransactionType>,
  ) {}

  async run() {
    const count = await this.repository.count();

    if (count === 0) {
      const players = await this.playerRepository.find();
      const transactionTypes = await this.transactionTypeRepository.find();

      const transactions = [];
      for (let x = 0; x < 500; x++) {
        const p = Math.floor(Math.random() * players.length);
        const type = Math.floor(Math.random() * transactionTypes.length);

        const t = this.repository.create({
          amount: Math.random() * 50,
          player: players[p],
          type: transactionTypes[type],
          note: faker.hacker.phrase(),
          date: faker.date.recent(90).toISOString(),
        });
        transactions.push(t);
      }

      await this.repository.save(transactions);
    }
  }
}
