import { Injectable, Inject } from '@nestjs/common';
import {
  GetReportSnapshotQuery,
  GetReportSnapshotByIdQuery,
  GetLatestReportSnapshotQuery,
} from './get-report-snapshot.query';
import {
  IReportSnapshotRepository,
  REPORT_SNAPSHOT_REPOSITORY,
} from '../../../domain/repositories';
import { ReportSnapshot } from '../../../domain/aggregates/report-snapshot';

@Injectable()
export class GetReportSnapshotHandler {
  constructor(
    @Inject(REPORT_SNAPSHOT_REPOSITORY)
    private readonly snapshotRepo: IReportSnapshotRepository,
  ) {}

  async execute(query: GetReportSnapshotQuery): Promise<ReportSnapshot | null> {
    return this.snapshotRepo.findByCodeAndPeriodKey(
      query.reportCode,
      query.periodKey,
    );
  }

  async executeById(
    query: GetReportSnapshotByIdQuery,
  ): Promise<ReportSnapshot | null> {
    return this.snapshotRepo.findById(query.id);
  }

  async executeLatest(
    query: GetLatestReportSnapshotQuery,
  ): Promise<ReportSnapshot | null> {
    return this.snapshotRepo.findLatestByCode(query.reportCode);
  }
}
