import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { SchedulingEvent, type RescheduleTrigger } from './entities/scheduling-event.entity';

@Injectable()
export class SchedulingRescheduleRepository {
  constructor(
    @InjectRepository(SchedulingEvent)
    private readonly repository: Repository<SchedulingEvent>,
  ) {}

  /** Persist an audit record for a completed reschedule. */
  async createEvent(data: {
    trainScheduleId: string;
    trigger: RescheduleTrigger;
    actorUserId?: string;
    reason?: string;
    planSnapshot: Record<string, unknown>;
    displacedBookingIds: string[];
  }): Promise<SchedulingEvent> {
    return this.repository.save(this.repository.create(data));
  }
}
