export type BookingTripType = "one-way" | "round-trip";

export const BOOKING_TRIP_TYPE_LABEL: Record<BookingTripType, string> = {
  "one-way": "Một chiều",
  "round-trip": "Khứ hồi",
};

export type SeatClass = "eco" | "vip";

export const SEAT_CLASS_OPTIONS: { id: SeatClass; label: string }[] = [
  { id: "eco", label: "Eco" },
  { id: "vip", label: "VIP" },
];

export type TripAvailability = "available" | "sold_out";

export type ApiRoute = {
  id: string;
  slug: string;
  label: string;
  reverseRouteId: number | null;
  reverseRouteSlug: string | null;
};

export type BookingRouteOption = {
  id: string;
  label: string;
  href: string;
  legA: string;
  legB: string;
  operator: string;
  basePriceVnd: number;
};

export type BookingRouteSelectOption = BookingRouteOption & {
  baseRouteId: string;
  direction: "a-to-b" | "b-to-a";
};

export type MockTrip = {
  id: string;
  routeId: string;
  direction: "a-to-b" | "b-to-a";
  routeLabel: string;
  date: string;
  departure: string;
  arrival: string;
  operator: string;
  operatorLogo?: string;
  priceVnd: number;
  priceChildVnd: number;
  priceSeniorVnd: number;
  availability: TripAvailability;
  seatsRemaining?: number;
};

export const BOOKING_WIZARD_STEPS = [
  { id: 1, label: "CHUYẾN TÀU" },
  { id: 2, label: "LIÊN HỆ" },
  { id: 3, label: "HÀNH KHÁCH" },
  { id: 4, label: "THANH TOÁN" },
] as const;

export const DEFAULT_BOOKING_SEARCH = {
  tripType: "round-trip" as BookingTripType,
  seatClass: "eco" as SeatClass,
  routeId: "rach-gia-phu-quoc",
  passengers: 1,
};

function splitRouteLabel(label: string): { legA: string; legB: string; short: string } {
  const parts = label.split(/\s*[-–—]\s*/);
  if (parts.length >= 2) {
    const legA = parts[0].trim().toUpperCase();
    const legB = parts.slice(1).join(" - ").trim().toUpperCase();
    return { legA, legB, short: `${legA} - ${legB}` };
  }
  const up = label.trim().toUpperCase();
  return { legA: up, legB: up, short: up };
}

export function routesToBookingOptions(routes: ApiRoute[]): BookingRouteOption[] {
  return routes.map((route) => {
    const { legA, legB, short } = splitRouteLabel(route.label);
    return {
      id: route.slug,
      label: short,
      href: `/${route.slug}/`,
      legA,
      legB,
      operator: "Phú Quốc Express",
      basePriceVnd: 315_000,
    };
  });
}

export function getRouteSelectOptions(
  _tripType: BookingTripType,
  routeOptions: BookingRouteOption[],
): BookingRouteSelectOption[] {
  // Mỗi chiều đã là một tuyến trong admin — không tạo thêm bản -rev (trùng nhãn).
  return routeOptions.map((route) => ({
    ...route,
    baseRouteId: route.id,
    direction: "a-to-b",
  }));
}

export function parseRouteSelection(routeId: string): {
  baseRouteId: string;
  direction: "a-to-b" | "b-to-a";
} {
  if (routeId.endsWith("-rev")) {
    return { baseRouteId: routeId.slice(0, -4), direction: "b-to-a" };
  }
  return { baseRouteId: routeId, direction: "a-to-b" };
}

export function resolveTripRouteSlugs(
  routes: ApiRoute[],
  routeId: string,
  tripType: BookingTripType,
): { outboundSlug: string; inboundSlug: string | null } {
  const { baseRouteId, direction } = parseRouteSelection(routeId);
  const base = routes.find((r) => r.slug === baseRouteId);

  const forwardSlug = baseRouteId;
  const reverseSlug = base?.reverseRouteSlug ?? null;

  if (tripType === "round-trip") {
    return {
      outboundSlug: forwardSlug,
      inboundSlug: reverseSlug,
    };
  }

  if (direction === "b-to-a") {
    return {
      outboundSlug: reverseSlug ?? forwardSlug,
      inboundSlug: null,
    };
  }

  return { outboundSlug: forwardSlug, inboundSlug: null };
}

export { DEFAULT_NATIONALITY, NATIONALITY_OPTIONS } from "@/config/nationality-options";
