import { z } from "zod"

import { Lang } from "@/constants/languages"

export const getBreadcrumbsSchema = z.array(
  z.object({
    href: z.string().optional(),
    title: z.string(),
    uid: z.string(),
  })
)

const breadcrumbsRefsItems = z.object({
  items: z.array(
    z.object({
      web: z
        .object({
          breadcrumbs: z
            .object({
              title: z.string(),
              parentsConnection: z.object({
                edges: z.array(
                  z.object({
                    node: z.object({
                      system: z.object({
                        content_type_uid: z.string(),
                        uid: z.string(),
                      }),
                    }),
                  })
                ),
              }),
            })
            .optional(),
        })
        .optional(),
      system: z.object({
        content_type_uid: z.string(),
        uid: z.string(),
      }),
    })
  ),
})

export type BreadcrumbsRefsItems = z.infer<typeof breadcrumbsRefsItems>

export const validateMyPagesBreadcrumbsRefsContentstackSchema = z.object({
  all_account_page: breadcrumbsRefsItems,
})

export type GetMyPagesBreadcrumbsRefsData = z.infer<
  typeof validateMyPagesBreadcrumbsRefsContentstackSchema
>

export const validateLoyaltyPageBreadcrumbsRefsContentstackSchema = z.object({
  all_loyalty_page: breadcrumbsRefsItems,
})

export type GetLoyaltyPageBreadcrumbsRefsData = z.infer<
  typeof validateLoyaltyPageBreadcrumbsContentstackSchema
>

const page = z.object({
  web: z.object({
    breadcrumbs: z.object({
      title: z.string(),
      parentsConnection: z.object({
        edges: z.array(
          z.object({
            node: z.object({
              web: z.object({
                breadcrumbs: z.object({
                  title: z.string(),
                }),
              }),
              system: z.object({
                locale: z.nativeEnum(Lang),
                uid: z.string(),
              }),
              url: z.string(),
            }),
          })
        ),
      }),
    }),
  }),
  system: z.object({
    uid: z.string(),
  }),
})

export type Page = z.infer<typeof page>

const breadcrumbsItems = z.object({
  items: z.array(page),
})

export const validateMyPagesBreadcrumbsContentstackSchema = z.object({
  all_account_page: breadcrumbsItems,
})

export type GetMyPagesBreadcrumbsData = z.infer<
  typeof validateMyPagesBreadcrumbsContentstackSchema
>

export const validateLoyaltyPageBreadcrumbsContentstackSchema = z.object({
  all_loyalty_page: breadcrumbsItems,
})

export type GetLoyaltyPageBreadcrumbsData = z.infer<
  typeof validateLoyaltyPageBreadcrumbsContentstackSchema
>
