import { z } from "zod"

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

import {
  ContentEntries,
  DynamicContentComponents,
} from "@/types/components/myPages/myPage/enums"
import { Embeds } from "@/types/requests/embeds"
import { PageLinkEnum } from "@/types/requests/pageLinks"
import { Edges } from "@/types/requests/utils/edges"
import { RTEDocument } from "@/types/rte/node"

const accountPageShortcuts = z.object({
  __typename: z.literal(ContentEntries.AccountPageContentShortcuts),
  shortcuts: z.object({
    title: z.string().nullable(),
    preamble: z.string().nullable(),
    shortcuts: z.array(
      z.object({
        linkConnection: z.object({
          edges: z.array(
            z.object({
              node: z.object({
                system: z.object({
                  uid: z.string(),
                  locale: z.nativeEnum(Lang),
                }),
                original_url: z.string().nullable().optional(),
                url: z.string(),
                title: z.string(),
              }),
            })
          ),
          totalCount: z.number(),
        }),
        text: z.string().nullable(),
        open_in_new_tab: z.boolean(),
      })
    ),
  }),
})

const accountPageDynamicContent = z.object({
  __typename: z.literal(ContentEntries.AccountPageContentDynamicContent),
  dynamic_content: z.object({
    title: z.string().nullable(),
    preamble: z.string().nullable(),
    component: z.nativeEnum(DynamicContentComponents),
    link: z.object({
      linkConnection: z.object({
        edges: z.array(
          z.object({
            node: z.object({
              system: z.object({
                uid: z.string(),
                locale: z.nativeEnum(Lang),
              }),
              url: z.string(),
              original_url: z.string().nullable().optional(),
              title: z.string(),
            }),
          })
        ),
        totalCount: z.number(),
      }),
      link_text: z.string(),
    }),
  }),
})

const accountPageTextContent = z.object({
  __typename: z.literal(ContentEntries.AccountPageContentTextContent),
  text_content: z.object({
    content: z.object({
      json: z.any(),
      embedded_itemsConnection: z.object({
        edges: z.array(z.any()),
        totalCount: z.number(),
      }),
    }),
  }),
})

type TextContentRaw = z.infer<typeof accountPageTextContent>

type DynamicContentRaw = z.infer<typeof accountPageDynamicContent>

type ShortcutsRaw = z.infer<typeof accountPageShortcuts>

export type Shortcuts = Omit<ShortcutsRaw, "shortcuts"> & {
  shortcuts: Omit<ShortcutsRaw["shortcuts"], "shortcuts"> & {
    shortcuts: {
      text?: string
      openInNewTab: boolean
      url: string
      title: string
    }[]
  }
}

export type RteTextContent = Omit<TextContentRaw, "text_content"> & {
  text_content: {
    content: {
      json: RTEDocument
      embedded_itemsConnection: Edges<Embeds>
    }
  }
}

export type AccountPageContentItem =
  | DynamicContentRaw
  | Shortcuts
  | RteTextContent

const accountPageContentItem = z.discriminatedUnion("__typename", [
  accountPageShortcuts,
  accountPageDynamicContent,
  accountPageTextContent,
])

export const validateAccountPageSchema = z.object({
  account_page: z.object({
    url: z.string(),
    title: z.string(),
    content: z.array(accountPageContentItem),
  }),
})

export type AccountPageDataRaw = z.infer<typeof validateAccountPageSchema>

type AccountPageRaw = AccountPageDataRaw["account_page"]

export type AccountPage = Omit<AccountPageRaw, "content"> & {
  content: AccountPageContentItem[]
}

// Refs types
const pageConnectionRefs = z.object({
  edges: z.array(
    z.object({
      node: z.object({
        __typename: z.nativeEnum(PageLinkEnum),
        system: z.object({
          content_type_uid: z.string(),
          uid: z.string(),
        }),
      }),
    })
  ),
})

const accountPageShortcutsRefs = z.object({
  __typename: z.literal(ContentEntries.AccountPageContentShortcuts),
  shortcuts: z.object({
    shortcuts: z.array(
      z.object({
        linkConnection: pageConnectionRefs,
      })
    ),
  }),
})

const accountPageDynamicContentRefs = z.object({
  __typename: z.literal(ContentEntries.AccountPageContentDynamicContent),
  dynamic_content: z.object({
    link: z.object({
      linkConnection: pageConnectionRefs,
    }),
  }),
})

const accountPageContentItemRefs = z.discriminatedUnion("__typename", [
  accountPageDynamicContentRefs,
  accountPageShortcutsRefs,
])

export const validateAccountPageRefsSchema = z.object({
  account_page: z.object({
    content: z.array(accountPageContentItemRefs),
    system: z.object({
      content_type_uid: z.string(),
      uid: z.string(),
    }),
  }),
})

export type AccountPageRefsDataRaw = z.infer<
  typeof validateAccountPageRefsSchema
>
