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

import { TrainCheckpointEvent } from './entities/train-checkpoint-event.entity';

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

  findBySchedule(trainScheduleId: string): Promise<TrainCheckpointEvent[]> {
    return this.findAll({
      where: { trainScheduleId },
      relations: { yard: true },
      order: { sequenceNo: 'ASC', occurredAt: 'ASC' },
    });
  }
}
