import { JourneyDirection } from '../../modules/seats/seats.dto';

/**
 * Shared by SeatsService (seatmap display, hold-creation conflict checks) and
 * SegmentsService (search results' availability counts, EnhancedSeatsService) — the
 * single source of truth for whether two journey directions on the same schedule
 * should be treated as conflicting. Without this, a round-trip's OUTBOUND and RETURN
 * legs on the same schedule would wrongly block each other's seats.
 *
 * Check if two journey directions conflict (should not be allowed simultaneously).
 * For round-trip bookings: OUTBOUND and RETURN should NOT conflict on the same schedule.
 */
export function checkDirectionConflict(current: JourneyDirection, existing: JourneyDirection): boolean {
  // OUTBOUND and RETURN are allowed simultaneously (round-trip on the same schedule)
  if ((current === JourneyDirection.OUTBOUND && existing === JourneyDirection.RETURN) ||
      (current === JourneyDirection.RETURN && existing === JourneyDirection.OUTBOUND)) {
    return false;
  }

  // Same directions conflict (e.g., two OUTBOUND or two RETURN bookings)
  if (current === existing) {
    return true;
  }

  // ONE_WAY conflicts with other ONE_WAY bookings only
  if (current === JourneyDirection.ONE_WAY && existing === JourneyDirection.ONE_WAY) {
    return true;
  }

  // ONE_WAY with OUTBOUND/RETURN: conflict (to maintain safety for legacy bookings)
  if (current === JourneyDirection.ONE_WAY || existing === JourneyDirection.ONE_WAY) {
    return true;
  }

  // Default: no conflict
  return false;
}
