import { CurrencyEnum } from "@/types/enums/currency"
import type { AvailabilityError } from "@/types/stores/rates"
import type {
  Product,
  RoomConfiguration,
  RoomsAvailability,
} from "@/types/trpc/routers/hotel/roomAvailability"

export function findProduct(
  rateCode: string,
  product: Product,
  counterRateCode = ""
) {
  if ("corporateCheque" in product) {
    return product.corporateCheque.rateCode === rateCode
  }

  if ("redemption" in product) {
    return product.redemption.rateCode === rateCode
  }

  if ("voucher" in product) {
    return product.voucher.rateCode === rateCode
  }

  const memberExists = "member" in product
  const publicExists = "public" in product
  const isRegularRate = memberExists && publicExists
  if (isRegularRate) {
    let isProduct = false
    if (product.member) {
      isProduct =
        product.member.rateCode === rateCode ||
        product.member.rateCode === counterRateCode
    }
    if (product.public) {
      isProduct =
        product.public.rateCode === rateCode ||
        product.public.rateCode === counterRateCode
    }
    return isProduct
  }

  return null
}

export function findProductInRoom(
  rateCode: string | undefined | null,
  room: RoomConfiguration,
  counterRateCode: string | undefined | null
) {
  if (!rateCode) {
    return null
  }

  if (room.campaign.length) {
    const campaignProduct = room.campaign.find((product) =>
      findProduct(rateCode, product, counterRateCode || "")
    )
    if (campaignProduct) {
      return campaignProduct
    }
  }
  if (room.code.length) {
    const codeProduct = room.code.find((product) =>
      findProduct(rateCode, product, counterRateCode || "")
    )
    if (codeProduct) {
      return codeProduct
    }
  }
  if (room.redemptions.length) {
    const redemptionProduct = room.redemptions.find((product) =>
      findProduct(rateCode, product)
    )
    if (redemptionProduct) {
      return redemptionProduct
    }
  }
  if (room.regular.length) {
    const regularProduct = room.regular.find((product) =>
      findProduct(rateCode, product, counterRateCode || "")
    )
    if (regularProduct) {
      return regularProduct
    }
  }
}

export function findSelectedRate(
  rateCode: string | undefined | null,
  counterRateCode: string | undefined | null,
  roomTypeCode: string | undefined | null,
  rooms: RoomConfiguration[] | AvailabilityError
) {
  if (!Array.isArray(rooms)) {
    return null
  }

  if (!rateCode) {
    return null
  }

  return rooms.find((room) => {
    if (room.roomTypeCode !== roomTypeCode) {
      return false
    }
    return findProductInRoom(rateCode, room, counterRateCode)
  })
}

export function findDefaultCurrency(
  roomsAvailability: (RoomsAvailability | AvailabilityError)[] | undefined
) {
  if (!roomsAvailability || !roomsAvailability.length) {
    return CurrencyEnum.Unknown
  }

  const availability = roomsAvailability.filter(
    (room): room is RoomsAvailability => {
      if ("error" in room) {
        return false
      }
      return true
    }
  )[0]

  const pkg = availability?.packages.find((pkg) => pkg.localPrice.currency)
  if (!pkg) {
    return CurrencyEnum.Unknown
  }

  const defaultCurrency = pkg.localPrice.currency
  return defaultCurrency
}
