import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';

export interface SearchCriteria {
  originStationId: string;
  destinationStationId: string;
  departureDate: string;
  returnDate?: string;
  tripType: 'ONE_WAY' | 'ROUND_TRIP';
  adultCount: number;
  childCount: number;
  nationality: 'ETHIOPIAN' | 'DJIBOUTIAN' | 'OTHER';
  promoCode?: string;
}

export interface PassengerDetail {
  name: string;
  dateOfBirth: string;
  nationality: string;
  faydaVerified?: boolean;
  nationalId?: string;
  faydaSub?: string;
  passportNumber?: string;
  passportCountry?: string;
  passportIssueDate?: string;
  passportExpiryDate?: string;
  passportIssuingAuthority?: string;
  idDocumentType?: string;
  isPrimaryPassenger: boolean;
  seatId?: string;
  seatNumber?: string;
  phone?: string;
  email?: string;
  gender?: string;
}

export interface SelectedSchedule {
  id: string;
  trainNumber: string;
  origin: string;
  destination: string;
  departureTime: string;
  arrivalTime: string;
  duration: string;
  baseFareAdult: number;
  baseFareChild: number;
  selectedSeatClass?: string;
  selectedSeatClassName?: string;
}

export interface SeatHold {
  holdId: string;
  expiresAt: string;
}

interface BookingState {
  searchCriteria: SearchCriteria | null;
  selectedSchedule: SelectedSchedule | null;
  outboundSchedule: SelectedSchedule | null;
  inboundSchedule: SelectedSchedule | null;
  passengers: PassengerDetail[];
  seatHold: SeatHold | null;
  bookingId: string | null;
  pnr: string | null;
  selectedPaymentMethod: string | null;
  createAccount: boolean;
  passengerId: string | null;
  
  setSearchCriteria: (criteria: SearchCriteria) => void;
  setSelectedSchedule: (schedule: SelectedSchedule) => void;
  setOutboundSchedule: (schedule: SelectedSchedule) => void;
  setInboundSchedule: (schedule: SelectedSchedule) => void;
  setPassengers: (passengers: PassengerDetail[]) => void;
  setSeatHold: (hold: SeatHold | null) => void;
  setBookingId: (id: string) => void;
  setPNR: (pnr: string) => void;
  setPaymentMethod: (method: string) => void;
  setCreateAccount: (create: boolean) => void;
  setPassengerId: (id: string | null) => void;
  clearBooking: () => void;
}

export const useBookingStore = create<BookingState>()(persist(
  (set) => (({
      searchCriteria: null,
      selectedSchedule: null,
      outboundSchedule: null,
      inboundSchedule: null,
      passengers: [],
      seatHold: null,
      bookingId: null,
      pnr: null,
      selectedPaymentMethod: null,
      createAccount: false,
      passengerId: null,

      setSearchCriteria: (criteria) => set({ searchCriteria: criteria }),
      setSelectedSchedule: (schedule) => set({ selectedSchedule: schedule }),
      setOutboundSchedule: (schedule) => set({ outboundSchedule: schedule }),
      setInboundSchedule: (schedule) => set({ inboundSchedule: schedule }),
      setPassengers: (passengers) => set({ passengers }),
      setSeatHold: (hold) => set({ seatHold: hold }),
      setBookingId: (id) => set({ bookingId: id }),
      setPNR: (pnr) => set({ pnr }),
      setPaymentMethod: (method) => set({ selectedPaymentMethod: method }),
      setCreateAccount: (create) => set({ createAccount: create }),
      setPassengerId: (id) => set({ passengerId: id }),
      clearBooking: () => set({
        searchCriteria: null,
        selectedSchedule: null,
        outboundSchedule: null,
        inboundSchedule: null,
        passengers: [],
        seatHold: null,
        bookingId: null,
        pnr: null,
        selectedPaymentMethod: null,
        createAccount: false,
        passengerId: null,
      }),
  } as BookingState)),
  {
    name: 'booking-storage',
    storage: createJSONStorage(() => {
      if (typeof window !== 'undefined') {
        return localStorage;
      }
      return {
        getItem: () => null,
        setItem: () => {},
        removeItem: () => {},
      };
    }),
  }
));
