import type { BookingTripType, MockTrip } from "@/config/booking-config";

export type BookingSearchInput = {
  routeId: string;
  tripType: BookingTripType;
  departDate: string;
  returnDate: string;
  passengers: number;
};

export type BookingSearchResult = {
  outbound: MockTrip[];
  inbound: MockTrip[];
};

export function filterTripsForPassengers(trips: MockTrip[], passengers: number): MockTrip[] {
  return trips.map((trip) => {
    const seats = trip.seatsRemaining ?? 0;
    if (trip.availability === "sold_out" || seats < passengers) {
      return { ...trip, availability: "sold_out" as const };
    }
    return trip;
  });
}

export function formatVnd(amount: number): string {
  return `${amount.toLocaleString("vi-VN")} VND`;
}

/** Trips use dd-mm-yyyy; native date input uses yyyy-mm-dd. */
export function displayDateToIso(value: string): string {
  const match = value.match(/^(\d{2})-(\d{2})-(\d{4})$/);
  if (!match) return "";
  return `${match[3]}-${match[2]}-${match[1]}`;
}

export function isoDateToDisplay(value: string): string {
  const match = value.match(/^(\d{4})-(\d{2})-(\d{2})$/);
  if (!match) return "";
  return `${match[3]}-${match[2]}-${match[1]}`;
}

export function isoToDatePickerDisplay(value: string): string {
  const match = value.match(/^(\d{4})-(\d{2})-(\d{2})$/);
  if (!match) return "";
  return `${match[3]}/${match[2]}/${match[1]}`;
}

export function parseDisplayDate(value: string): Date | null {
  const trimmed = value.trim();
  const match = trimmed.match(/^(\d{1,2})[-/](\d{1,2})[-/](\d{4})$/);
  if (!match) return null;
  const day = Number(match[1]);
  const month = Number(match[2]);
  const year = Number(match[3]);
  const date = new Date(year, month - 1, day);
  if (
    Number.isNaN(date.getTime()) ||
    date.getFullYear() !== year ||
    date.getMonth() !== month - 1 ||
    date.getDate() !== day
  ) {
    return null;
  }
  return date;
}

export function formatDisplayDate(date: Date): string {
  const day = String(date.getDate()).padStart(2, "0");
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const year = date.getFullYear();
  return `${day}-${month}-${year}`;
}

function addDays(date: Date, days: number): Date {
  const next = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  next.setDate(next.getDate() + days);
  return next;
}

/** Ngày đi mặc định: hôm nay (theo giờ trình duyệt). */
export function defaultDepartDateDisplay(): string {
  return formatDisplayDate(new Date());
}

/** Ngày về mặc định: ngày sau ngày đi (khứ hồi). */
export function defaultReturnDateDisplay(): string {
  return formatDisplayDate(addDays(new Date(), 1));
}

export function todayIsoDate(): string {
  const d = new Date();
  const y = d.getFullYear();
  const m = String(d.getMonth() + 1).padStart(2, "0");
  const day = String(d.getDate()).padStart(2, "0");
  return `${y}-${m}-${day}`;
}

/** So sánh yyyy-mm-dd. */
export function compareIsoDates(a: string, b: string): number {
  return a.localeCompare(b);
}

export function isoToMonthKey(iso: string): string {
  return iso.slice(0, 7);
}
