import { z } from "zod"

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

import { essentialsBlockSchema } from "../schemas/blocks/essentials"
import { systemSchema } from "../schemas/system"

import { CampaignPageEnum } from "@/types/enums/campaignPage"

const campaignPageEssentials = z
  .object({
    __typename: z.literal(CampaignPageEnum.ContentStack.blocks.Essentials),
  })
  .merge(essentialsBlockSchema)

export const blocksSchema = z.discriminatedUnion("__typename", [
  campaignPageEssentials,
])

export const campaignPageSchema = z.object({
  campaign_page: z.object({
    title: z.string(),
    campaign_identifier: z.string().nullish(),
    heading: z.string(),
    subheading: z.string().nullish(),
    preamble: z.object({
      is_two_columns: z.boolean().default(false),
      first_column: z.string(),
      second_column: z.string(),
    }),
    blocks: discriminatedUnionArray(blocksSchema.options),
    system: systemSchema.merge(
      z.object({
        created_at: z.string(),
        updated_at: z.string(),
      })
    ),
  }),
  trackingProps: z.object({
    url: z.string(),
  }),
})

export const campaignPageRefsSchema = z.object({
  campaign_page: z.object({
    system: systemSchema,
  }),
})
