import { z } from "zod"

import { discriminatedUnionArray } from "@/lib/discriminatedUnion"

import {
  cardGridRefsSchema,
  cardsGridSchema,
} from "../schemas/blocks/cardsGrid"
import {
  carouselCardsRefsSchema,
  carouselCardsSchema,
} from "../schemas/blocks/carouselCards"
import {
  fullWidthCampaignBlockRefsSchema,
  fullWidthCampaignBlockSchema,
} from "../schemas/blocks/fullWidthCampaign"
import {
  joinScandicFriendsBlockRefsSchema,
  joinScandicFriendsBlockSchema,
} from "../schemas/blocks/joinScandicFriends"
import { tempImageVaultAssetSchema } from "../schemas/imageVault"
import { systemSchema } from "../schemas/system"

import { StartPageEnum } from "@/types/enums/startPage"

const startPageCards = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.CardsGrid),
  })
  .merge(cardsGridSchema)

const startPageCarouselCards = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.CarouselCards),
  })
  .merge(carouselCardsSchema)

const startPageFullWidthCampaign = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.FullWidthCampaign),
  })
  .merge(fullWidthCampaignBlockSchema)

const startPageJoinScandicFriends = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.JoinScandicFriends),
  })
  .merge(joinScandicFriendsBlockSchema)

export const blocksSchema = z.discriminatedUnion("__typename", [
  startPageCards,
  startPageFullWidthCampaign,
  startPageCarouselCards,
  startPageJoinScandicFriends,
])

export const startPageSchema = z.object({
  start_page: z.object({
    title: z.string(),
    header: z.object({
      heading: z.string(),
      hero_image: tempImageVaultAssetSchema,
    }),
    blocks: discriminatedUnionArray(blocksSchema.options)
      .nullable()
      .transform((val) => val || []),
    system: systemSchema.merge(
      z.object({
        created_at: z.string(),
        updated_at: z.string(),
      })
    ),
  }),
  trackingProps: z.object({
    url: z.string(),
  }),
})

/** REFS */
const startPageCardsRefs = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.CardsGrid),
  })
  .merge(cardGridRefsSchema)

const startPageFullWidthCampaignRef = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.FullWidthCampaign),
  })
  .merge(fullWidthCampaignBlockRefsSchema)

const startPageCarouselCardsRef = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.CarouselCards),
  })
  .merge(carouselCardsRefsSchema)

const startPageJoinScandicFriendsRef = z
  .object({
    __typename: z.literal(StartPageEnum.ContentStack.blocks.JoinScandicFriends),
  })
  .merge(joinScandicFriendsBlockRefsSchema)

const startPageBlockRefsItem = z.discriminatedUnion("__typename", [
  startPageCardsRefs,
  startPageFullWidthCampaignRef,
  startPageCarouselCardsRef,
  startPageJoinScandicFriendsRef,
])

export const startPageRefsSchema = z.object({
  start_page: z.object({
    blocks: discriminatedUnionArray(startPageBlockRefsItem.options).nullable(),
    system: systemSchema,
  }),
})
