"use client"

import { differenceInCalendarDays, isWeekend } from "date-fns"
import { usePathname } from "next/navigation"
import { useEffect } from "react"

import { useSessionId } from "@scandic-hotels/common/hooks/useSessionId"
import {
  TrackingChannelEnum,
  type TrackingSDKHotelInfo,
  type TrackingSDKPageData,
} from "@scandic-hotels/tracking/types"

import { trackEvent } from "./base"
import { createSDKPageObject } from "./pageview"

import type { Lang } from "@scandic-hotels/common/constants/language"

type NoAvailabilityTrackingProps = {
  lang: Lang
  shouldTrackNoAvailability: boolean
  hotelsTrackingData: TrackingSDKHotelInfo
  pageTrackingData: TrackingSDKPageData
}

export function NoAvailabilityTracking({
  lang,
  shouldTrackNoAvailability,
  hotelsTrackingData,
  pageTrackingData,
}: NoAvailabilityTrackingProps) {
  const pathName = usePathname()
  const sessionId = useSessionId()

  useEffect(() => {
    if (!shouldTrackNoAvailability) {
      return
    }
    const pageTracking = {
      ...pageTrackingData,
      sessionId,
      pathName,
    }
    const pageObject = createSDKPageObject(pageTracking)
    const {
      searchTerm,
      searchType,
      hotelID,
      leadTime,
      noOfRooms,
      bookingCode,
      rewardNight,
      bookingTypeofDay,
      duration,
      specialRoomType,
    } = hotelsTrackingData

    trackEvent({
      event: "noRoomsAvailable",
      hotelInfo: {
        searchTerm,
        searchType,
        action: "Hotel search",
        noRoomsAvailable: "yes",
        hotelID,
        leadTime,
        noOfRooms,
        bookingCode,
        rewardNight,
        bookingTypeofDay,
        duration,
        specialRoomType,
      },
      pageInfo: pageObject,
    })
  }, [lang, hotelsTrackingData, pageTrackingData])

  return null
}

type TrackNoAvailabilityParams = {
  specialRoomType?: string
  fromDate: string
  toDate: string
  hotelId?: string
  noOfRooms?: number
  searchType?: string
  bookingCode?: string
  searchTerm: string
  rewardNight: string
  pageId: string
  pageName: string
  pageType: string
  siteSections: string
  lang: Lang
  domain?: string
}

export function trackNoAvailability({
  specialRoomType,
  lang,
  fromDate,
  toDate,
  hotelId,
  noOfRooms,
  searchType,
  bookingCode,
  searchTerm,
  rewardNight,
  pageId,
  pageName,
  pageType,
  siteSections,
  domain,
}: TrackNoAvailabilityParams) {
  const arrivalDate = new Date(fromDate)
  const departureDate = new Date(toDate)

  trackEvent({
    event: "noRoomsAvailable",
    hotelInfo: {
      searchTerm,
      searchType,
      action: "Hotel search",
      noRoomsAvailable: "yes",
      hotelId,
      leadTime: differenceInCalendarDays(arrivalDate, new Date()),
      noOfRooms,
      bookingCode: bookingCode ?? "n/a",
      rewardNight,
      bookingTypeofDay: isWeekend(arrivalDate) ? "weekend" : "weekday",
      duration: differenceInCalendarDays(departureDate, arrivalDate),
      specialRoomType,
    },
    pageInfo: {
      channel: TrackingChannelEnum.hotelreservation,
      domain,
      domainLanguage: lang,
      pageId,
      pageName,
      pageType,
      siteSections,
    },
  })
}
