import type {
  CabinetEventsRepository,
  CreateCabinetEventData,
} from './cabinet-events.repository.js';
import type { CabinetEventQueryInput, SpendingSummaryQueryInput } from '@meshitrack/shared';

interface Deps {
  cabinetEventsRepository: CabinetEventsRepository;
}

export class CabinetEventsService {
  private readonly cabinetEventsRepository: CabinetEventsRepository;

  public constructor({ cabinetEventsRepository }: Deps) {
    this.cabinetEventsRepository = cabinetEventsRepository;
  }

  public async logEvent(data: CreateCabinetEventData) {
    return this.cabinetEventsRepository.create(data);
  }

  public async logEvents(events: CreateCabinetEventData[]) {
    if (events.length === 0) return [];
    return this.cabinetEventsRepository.createMany(events);
  }

  public async listEvents(householdId: string, query: CabinetEventQueryInput) {
    return this.cabinetEventsRepository.findByHousehold(householdId, query);
  }

  public async getEventsByItem(
    householdId: string,
    cabinetItemId: string,
    query: { cursor?: string; limit: number },
  ) {
    return this.cabinetEventsRepository.findByCabinetItem(householdId, cabinetItemId, query);
  }

  public async getSpendingSummary(householdId: string, query: SpendingSummaryQueryInput) {
    return this.cabinetEventsRepository.getSpendingSummary(householdId, query);
  }

  public async getAvgUnitPrices(householdId: string, medicineIds: string[]) {
    return this.cabinetEventsRepository.getAvgUnitPriceByMedicine(householdId, medicineIds);
  }
}
