import {
  GetMyPagesBreadcrumbs,
  GetMyPagesBreadcrumbsRefs,
} from "@/lib/graphql/Query/Breadcrumbs/AccountPage.graphql"
import {
  GetCollectionPageBreadcrumbs,
  GetCollectionPageBreadcrumbsRefs,
} from "@/lib/graphql/Query/Breadcrumbs/CollectionPage.graphql"
import {
  GetContentPageBreadcrumbs,
  GetContentPageBreadcrumbsRefs,
} from "@/lib/graphql/Query/Breadcrumbs/ContentPage.graphql"
import {
  GetLoyaltyPageBreadcrumbs,
  GetLoyaltyPageBreadcrumbsRefs,
} from "@/lib/graphql/Query/Breadcrumbs/LoyaltyPage.graphql"
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"

import {
  GetCollectionPageBreadcrumbsData,
  GetCollectionPageBreadcrumbsRefsData,
  type GetContentPageBreadcrumbsData,
  type GetContentPageBreadcrumbsRefsData,
  type GetLoyaltyPageBreadcrumbsData,
  type GetLoyaltyPageBreadcrumbsRefsData,
  type GetMyPagesBreadcrumbsData,
  type GetMyPagesBreadcrumbsRefsData,
  validateCollectionPageBreadcrumbsContentstackSchema,
  validateCollectionPageBreadcrumbsRefsContentstackSchema,
  validateContentPageBreadcrumbsContentstackSchema,
  validateContentPageBreadcrumbsRefsContentstackSchema,
  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.error(
      `Failed to validate Loyaltypage Breadcrumbs Refs - (uid: ${variables.uid})`
    )
    console.error(validatedRefsData.error)
    return null
  }

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

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

  if (!response.data.loyalty_page.web?.breadcrumbs?.title) {
    return null
  }

  const validatedBreadcrumbsData =
    validateLoyaltyPageBreadcrumbsContentstackSchema.safeParse(response.data)

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

  return getBreadcrumbs(
    validatedBreadcrumbsData.data.loyalty_page,
    variables.locale
  )
}

async function getCollectionPageBreadcrumbs(variables: Variables) {
  const refsResponse =
    await getRefsResponse<GetCollectionPageBreadcrumbsRefsData>(
      GetCollectionPageBreadcrumbsRefs,
      variables
    )
  const validatedRefsData =
    validateCollectionPageBreadcrumbsRefsContentstackSchema.safeParse(
      refsResponse.data
    )

  if (!validatedRefsData.success) {
    console.error(
      `Failed to validate CollectionPpage Breadcrumbs Refs - (uid: ${variables.uid})`
    )
    console.error(validatedRefsData.error)
    return null
  }
  const tags = getTags(validatedRefsData.data.collection_page, variables)
  const response = await getResponse<GetCollectionPageBreadcrumbsData>(
    GetCollectionPageBreadcrumbs,
    variables,
    tags
  )
  if (!response.data.collection_page.web?.breadcrumbs?.title) {
    return null
  }
  const validatedBreadcrumbsData =
    validateCollectionPageBreadcrumbsContentstackSchema.safeParse(response.data)
  if (!validatedBreadcrumbsData.success) {
    console.error(
      `Failed to validate Collectionpage Breadcrumbs Data - (uid: ${variables.uid})`
    )
    console.error(validatedBreadcrumbsData.error)
    return null
  }
  return getBreadcrumbs(
    validatedBreadcrumbsData.data.collection_page,
    variables.locale
  )
}

async function getContentPageBreadcrumbs(variables: Variables) {
  const refsResponse = await getRefsResponse<GetContentPageBreadcrumbsRefsData>(
    GetContentPageBreadcrumbsRefs,
    variables
  )

  const validatedRefsData =
    validateContentPageBreadcrumbsRefsContentstackSchema.safeParse(
      refsResponse.data
    )

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

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

  const response = await getResponse<GetContentPageBreadcrumbsData>(
    GetContentPageBreadcrumbs,
    variables,
    tags
  )

  if (!response.data.content_page.web?.breadcrumbs?.title) {
    return null
  }

  const validatedBreadcrumbsData =
    validateContentPageBreadcrumbsContentstackSchema.safeParse(response.data)

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

  return getBreadcrumbs(
    validatedBreadcrumbsData.data.content_page,
    variables.locale
  )
}

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

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

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

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

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

  const validatedBreadcrumbsData =
    validateMyPagesBreadcrumbsContentstackSchema.safeParse(response.data)

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

  return getBreadcrumbs(
    validatedBreadcrumbsData.data.account_page,
    variables.locale
  )
}

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

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