import { createContext } from 'react';

import { StoreProduct } from '@medusajs/types';
import { noop } from 'lodash';

import { Tables } from '@kit/supabase/database';

export type Location = Tables<
  { schema: 'medreport' },
  'connected_online_locations'
>;

export type TimeSlotResponse = {
  timeSlots: TimeSlot[];
  locations: Location[];
};

export type TimeSlot = {
  ClinicID: number;
  LocationID: number;
  UserID: number;
  SyncUserID: number;
  ServiceID: number;
  HKServiceID: number;
  StartTime: Date;
  EndTime: Date;
  PayorCode: string;
  serviceProvider?: ServiceProvider;
  syncedService?: SyncedService;
} & { location?: Location };

export type ServiceProvider = {
  name: string;
  id: number;
  jobTitleEn: string | null;
  jobTitleEt: string | null;
  jobTitleRu: string | null;
  clinicId: number;
};

export type SyncedService = Tables<
  { schema: 'medreport' },
  'connected_online_services'
> & {
  providerClinic: ProviderClinic;
};

export type ProviderClinic = Tables<
  { schema: 'medreport' },
  'connected_online_providers'
> & { locations: Location[] };

const BookingContext = createContext<{
  timeSlots: TimeSlot[] | null;
  selectedService: StoreProduct | null;
  locations: Location[] | null;
  selectedLocationId: number | null;
  selectedDate?: Date;
  isLoadingTimeSlots?: boolean;
  setSelectedService: (selectedService: StoreProduct | null) => void;
  setSelectedLocationId: (selectedLocationId: number | null) => void;
  updateTimeSlots: (serviceIds: number[]) => Promise<void>;
  setSelectedDate: (selectedDate?: Date) => void;
}>({
  timeSlots: null,
  selectedService: null,
  locations: null,
  selectedLocationId: null,
  selectedDate: new Date(),
  isLoadingTimeSlots: false,
  setSelectedService: (_) => _,
  setSelectedLocationId: (_) => _,
  updateTimeSlots: async (_) => noop(),
  setSelectedDate: (_) => _,
});

export { BookingContext };
