import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { getCacheInstance } from '../../../shared/helpers/index.js';
import { DBProvider } from '../../app-providers/db.provider.js';
import type { IGetAllTaxVariablesQuery } from '../types.js';

const getAllTaxVariables = sql<IGetAllTaxVariablesQuery>`
  SELECT *
  FROM accounter_schema.business_trips_tax_variables
  ORDER BY date DESC;`;

@Injectable({
  scope: Scope.Singleton,
  global: true,
})
export class BusinessTripTaxVariablesProvider {
  cache = getCacheInstance({
    stdTTL: 60 * 5,
  });

  constructor(private dbProvider: DBProvider) {}

  private async batchTaxVariablesByDates(dates: readonly Date[]) {
    const taxVariables = await getAllTaxVariables.run(undefined, this.dbProvider);
    return dates.map(date => taxVariables.find(record => date.getTime() >= record.date.getTime()));
  }

  public getTaxVariablesByDateLoader = new DataLoader(
    (dates: readonly Date[]) => this.batchTaxVariablesByDates(dates),
    {
      cacheKeyFn: date => date.getTime(),
      cacheMap: this.cache,
    },
  );

  public clearCache() {
    this.cache.clear();
  }
}
