import { API_CONFIG } from '@/config/api'; // Extend API configuration for Channel Manager export const CHANNEL_MANAGER_API = { ...API_CONFIG, ENDPOINTS: { ...API_CONFIG.ENDPOINTS, // Listings Management LISTINGS: '/listings', LISTING_DETAIL: '/listings/:id', // Channel Management CHANNELS: '/channels', CHANNEL_CONNECT: '/channels/connect', CHANNEL_DISCONNECT: '/channels/:id/disconnect', CHANNEL_SYNC: '/channels/:id/sync', // Reservations (multi-type) RESERVATIONS: '/reservations', RESERVATION_DETAIL: '/reservations/:id', CREATE_RESERVATION: '/reservations', UPDATE_RESERVATION: '/reservations/:id', CANCEL_RESERVATION: '/reservations/:id/cancel', // Hotels HOTELS: '/hotels', HOTEL_ROOMS: '/hotels/:id/rooms', HOTEL_AVAILABILITY: '/hotels/:id/availability', HOTEL_RATES: '/hotels/:id/rates', // Restaurants RESTAURANTS: '/restaurants', RESTAURANT_TABLES: '/restaurants/:id/tables', RESTAURANT_AVAILABILITY: '/restaurants/:id/availability', RESTAURANT_MENU: '/restaurants/:id/menu', // Vehicles VEHICLES: '/vehicles', VEHICLE_AVAILABILITY: '/vehicles/:id/availability', VEHICLE_RATES: '/vehicles/:id/rates', // Flights FLIGHTS: '/flights', FLIGHT_SEARCH: '/flights/search', FLIGHT_BOOKING: '/flights/book', // Itineraries ITINERARIES: '/itineraries', ITINERARY_DETAIL: '/itineraries/:id', // Analytics & Reports ANALYTICS: '/analytics', REVENUE_REPORTS: '/analytics/revenue', OCCUPANCY_REPORTS: '/analytics/occupancy', CHANNEL_PERFORMANCE: '/analytics/channels', } }; // Channel Manager Service export class ChannelManagerService { private static async request(endpoint: string, options: RequestInit = {}) { const token = localStorage.getItem('auth_token'); // Create AbortController for timeout functionality const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), CHANNEL_MANAGER_API.TIMEOUT); const response = await fetch(`${CHANNEL_MANAGER_API.BASE_URL}${endpoint}`, { ...options, headers: { ...CHANNEL_MANAGER_API.DEFAULT_HEADERS, ...(token && { Authorization: `Bearer ${token}` }), ...options.headers, }, signal: controller.signal, }); clearTimeout(timeoutId); if (!response.ok) { throw new Error(`API Error: ${response.status} ${response.statusText}`); } // Handle 204 No Content responses if (response.status === 204) { return null; } return await response.json(); } // Listings Management static getListings = (type?: string) => this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.LISTINGS}${type ? `?type=${type}` : ''}`); static getListingDetail = (id: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.LISTING_DETAIL.replace(':id', id)); static createListing = (data: any) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.LISTINGS, { method: 'POST', body: JSON.stringify(data), }); static updateListing = (id: string, data: any) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.LISTING_DETAIL.replace(':id', id), { method: 'PUT', body: JSON.stringify(data), }); static deleteListing = (id: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.LISTING_DETAIL.replace(':id', id), { method: 'DELETE', }); // Channel Management static getChannels = () => this.request(CHANNEL_MANAGER_API.ENDPOINTS.CHANNELS); static connectChannel = (channelData: any) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.CHANNEL_CONNECT, { method: 'POST', body: JSON.stringify(channelData), }); static disconnectChannel = (channelId: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.CHANNEL_DISCONNECT.replace(':id', channelId), { method: 'DELETE', }); static syncChannel = (channelId: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.CHANNEL_SYNC.replace(':id', channelId), { method: 'POST', }); // Reservations Management static getReservations = (filters?: any) => { const queryParams = filters ? '?' + new URLSearchParams(filters).toString() : ''; return this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.RESERVATIONS}${queryParams}`); }; static getReservationDetail = (id: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.RESERVATION_DETAIL.replace(':id', id)); static createReservation = (data: any) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.CREATE_RESERVATION, { method: 'POST', body: JSON.stringify(data), }); static updateReservation = (id: string, data: any) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.UPDATE_RESERVATION.replace(':id', id), { method: 'PUT', body: JSON.stringify(data), }); static cancelReservation = (id: string, reason?: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.CANCEL_RESERVATION.replace(':id', id), { method: 'POST', body: JSON.stringify({ reason }), }); // Hotel specific methods static getHotelRooms = (hotelId: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.HOTEL_ROOMS.replace(':id', hotelId)); static getHotelAvailability = (hotelId: string, dateRange: { startDate: string; endDate: string }) => this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.HOTEL_AVAILABILITY.replace(':id', hotelId)}?${new URLSearchParams(dateRange)}`); static updateHotelRates = (hotelId: string, rates: any) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.HOTEL_RATES.replace(':id', hotelId), { method: 'PUT', body: JSON.stringify(rates), }); // Restaurant specific methods static getRestaurantTables = (restaurantId: string) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.RESTAURANT_TABLES.replace(':id', restaurantId)); static getRestaurantAvailability = (restaurantId: string, date: string) => this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.RESTAURANT_AVAILABILITY.replace(':id', restaurantId)}?date=${date}`); // Vehicle specific methods static getVehicleAvailability = (vehicleId: string, dateRange: { startDate: string; endDate: string }) => this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.VEHICLE_AVAILABILITY.replace(':id', vehicleId)}?${new URLSearchParams(dateRange)}`); // Flight methods static searchFlights = (searchParams: any) => this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.FLIGHT_SEARCH}?${new URLSearchParams(searchParams)}`); static bookFlight = (bookingData: any) => this.request(CHANNEL_MANAGER_API.ENDPOINTS.FLIGHT_BOOKING, { method: 'POST', body: JSON.stringify(bookingData), }); // Analytics methods static getAnalytics = (type: string, dateRange: { startDate: string; endDate: string }) => this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.ANALYTICS}/${type}?${new URLSearchParams(dateRange)}`); static getRevenueReports = (dateRange: { startDate: string; endDate: string }) => this.request(`${CHANNEL_MANAGER_API.ENDPOINTS.REVENUE_REPORTS}?${new URLSearchParams(dateRange)}`); static getChannelPerformance = () => this.request(CHANNEL_MANAGER_API.ENDPOINTS.CHANNEL_PERFORMANCE); } export default ChannelManagerService;