import {
  GetLoyaltyPageBreadcrumbs,
  GetLoyaltyPageBreadcrumbsRefs,
} from "@/lib/graphql/Query/BreadcrumbsLoyaltyPage.graphql"
import {
  GetMyPagesBreadcrumbs,
  GetMyPagesBreadcrumbsRefs,
} from "@/lib/graphql/Query/BreadcrumbsMyPages.graphql"
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"

import {
  type GetLoyaltyPageBreadcrumbsData,
  type GetLoyaltyPageBreadcrumbsRefsData,
  type GetMyPagesBreadcrumbsData,
  type GetMyPagesBreadcrumbsRefsData,
  validateLoyaltyPageBreadcrumbsContentstackSchema,
  validateLoyaltyPageBreadcrumbsRefsContentstackSchema,
  validateMyPagesBreadcrumbsContentstackSchema,
  validateMyPagesBreadcrumbsRefsContentstackSchema,
} from "./output"
import {
  getBreadcrumbs,
  getRefsResponse,
  getResponse,
  getTags,
  Variables,
} from "./utils"

import { PageTypeEnum } from "@/types/requests/pageType"

async function getLoyaltyPageBreadcrumbs(variables: Variables) {
  const refsResponse = await getRefsResponse<GetLoyaltyPageBreadcrumbsRefsData>(
    GetLoyaltyPageBreadcrumbsRefs,
    variables
  )

  const validatedRefsData =
    validateLoyaltyPageBreadcrumbsRefsContentstackSchema.safeParse(
      refsResponse.data
    )

  if (!validatedRefsData.success) {
    console.info(
      `Failed to validate Loyaltypage Breadcrumbs Refs - (url: ${variables.url})`
    )
    console.error(validatedRefsData.error)
    return null
  }

  const tags = getTags(validatedRefsData.data.all_loyalty_page, variables)

  const response = await getResponse<GetLoyaltyPageBreadcrumbsData>(
    GetLoyaltyPageBreadcrumbs,
    variables,
    tags
  )

  if (!response.data.all_loyalty_page.items[0].web?.breadcrumbs?.title) {
    return null
  }

  const validatedBreadcrumbsData =
    validateLoyaltyPageBreadcrumbsContentstackSchema.safeParse(response.data)

  if (!validatedBreadcrumbsData.success) {
    console.info(
      `Failed to validate Loyaltypage Breadcrumbs Data - (url: ${variables.url})`
    )
    console.error(validatedBreadcrumbsData.error)
    return null
  }

  return getBreadcrumbs(
    validatedBreadcrumbsData.data.all_loyalty_page.items[0],
    variables.locale
  )
}

async function getMyPagesBreadcrumbs(variables: Variables) {
  const refsResponse = await getRefsResponse<GetMyPagesBreadcrumbsRefsData>(
    GetMyPagesBreadcrumbsRefs,
    variables
  )

  const validatedRefsData =
    validateMyPagesBreadcrumbsRefsContentstackSchema.safeParse(
      refsResponse.data
    )
  if (!validatedRefsData.success) {
    console.info(
      `Failed to validate My Page Breadcrumbs Refs - (url: ${variables.url})`
    )
    console.error(validatedRefsData.error)
    return null
  }

  const tags = getTags(validatedRefsData.data.all_account_page, variables)

  const response = await getResponse<GetMyPagesBreadcrumbsData>(
    GetMyPagesBreadcrumbs,
    variables,
    tags
  )

  if (!response.data.all_account_page.items[0].web?.breadcrumbs?.title) {
    return []
  }

  const validatedBreadcrumbsData =
    validateMyPagesBreadcrumbsContentstackSchema.safeParse(response.data)

  if (!validatedBreadcrumbsData.success) {
    console.info(
      `Failed to validate My Page Breadcrumbs Data - (url: ${variables.url})`
    )
    console.error(validatedBreadcrumbsData.error)
    return null
  }

  return getBreadcrumbs(
    validatedBreadcrumbsData.data.all_account_page.items[0],
    variables.locale
  )
}

export const breadcrumbsQueryRouter = router({
  get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
    const variables = {
      locale: ctx.lang,
      url: ctx.pathname,
    }

    switch (ctx.contentType) {
      case PageTypeEnum.accountPage:
        return await getMyPagesBreadcrumbs(variables)
      case PageTypeEnum.loyaltyPage:
        return await getLoyaltyPageBreadcrumbs(variables)
      default:
        return []
    }
  }),
})
