import { z } from "zod"

import { tempImageVaultAssetSchema } from "../../imageVault"
import {
  accountPageSchema,
  collectionPageSchema,
  contentPageSchema,
  destinationCityPageSchema,
  destinationCountryPageSchema,
  destinationOverviewPageSchema,
  hotelPageSchema,
  loyaltyPageSchema,
  startPageSchema,
  transformPageLink,
} from "../../pageLinks"
import { systemSchema } from "../../system"
import { imageContainerSchema } from "../imageContainer"
import { sysAssetSchema } from "../sysAsset"
import { buttonSchema } from "../utils/buttonLinkSchema"
import { linkConnectionRefsSchema } from "../utils/linkConnection"

import { CardsEnum } from "@/types/enums/cards"

export const teaserCardBlockSchema = z.object({
  __typename: z.literal(CardsEnum.TeaserCard),
  heading: z.string().default(""),
  body_text: z.string().default(""),
  image: tempImageVaultAssetSchema,
  primary_button: buttonSchema,
  secondary_button: buttonSchema,
  has_primary_button: z.boolean().default(false),
  has_secondary_button: z.boolean().default(false),
  has_sidepeek_button: z.boolean().default(false),
  sidepeek_button: z
    .object({
      call_to_action_text: z.string().optional().default(""),
    })
    .optional(),
  sidepeek_content: z
    .object({
      heading: z.string(),
      content: z.object({
        json: z.any(),
        embedded_itemsConnection: z.object({
          edges: z.array(
            z.object({
              node: z
                .discriminatedUnion("__typename", [
                  imageContainerSchema,
                  sysAssetSchema,
                  accountPageSchema,
                  collectionPageSchema,
                  contentPageSchema,
                  destinationCityPageSchema,
                  destinationCountryPageSchema,
                  destinationOverviewPageSchema,
                  hotelPageSchema,
                  loyaltyPageSchema,
                  startPageSchema,
                ])
                .transform((data) => {
                  const link = transformPageLink(data)
                  if (link) {
                    return link
                  }
                  return data
                }),
            })
          ),
        }),
      }),
      has_primary_button: z.boolean().default(false),
      primary_button: buttonSchema,
      has_secondary_button: z.boolean().default(false),
      secondary_button: buttonSchema,
    })
    .optional()
    .transform((data) => {
      if (!data) {
        return
      }

      return {
        ...data,
        primary_button: data.has_primary_button
          ? data.primary_button
          : undefined,
        secondary_button: data.has_secondary_button
          ? data.secondary_button
          : undefined,
      }
    }),
  system: systemSchema,
})

export function transformTeaserCardBlock(
  card: typeof teaserCardBlockSchema._type
) {
  return {
    __typename: card.__typename,
    body_text: card.body_text,
    heading: card.heading,
    primaryButton: card.has_primary_button ? card.primary_button : undefined,
    secondaryButton: card.has_secondary_button
      ? card.secondary_button
      : undefined,
    sidePeekButton: card.has_sidepeek_button ? card.sidepeek_button : undefined,
    sidePeekContent: card.has_sidepeek_button
      ? card.sidepeek_content
      : undefined,
    image: card.image,
    system: card.system,
  }
}

export const teaserCardBlockRefsSchema = z.object({
  __typename: z.literal(CardsEnum.TeaserCard),
  primary_button: linkConnectionRefsSchema,
  secondary_button: linkConnectionRefsSchema,
  system: systemSchema,
})
