import { z } from "zod"

import * as pageLinks from "@/server/routers/contentstack/schemas/pageLinks"

import { tempImageVaultAssetSchema } from "../imageVault"
import { systemSchema } from "../system"
import { buttonSchema } from "./utils/buttonLinkSchema"

import { BlocksEnums } from "@/types/enums/blocks"

export const fullWidthCampaignSchema = z.object({
  full_width_campaign: z
    .object({
      full_width_campaignConnection: z.object({
        edges: z.array(
          z.object({
            node: z.object({
              background_image: tempImageVaultAssetSchema,
              heading: z.string().optional(),
              body_text: z.string().optional(),
              scripted_top_title: z.string().optional(),
              has_primary_button: z.boolean().default(false),
              primary_button: buttonSchema,
              has_secondary_button: z.boolean().default(false),
              secondary_button: buttonSchema,
              system: systemSchema,
            }),
          })
        ),
      }),
    })
    .transform((data) => {
      return data.full_width_campaignConnection.edges[0]?.node || null
    }),
})

export const fullWidthCampaignBlockSchema = z
  .object({
    typename: z
      .literal(BlocksEnums.block.FullWidthCampaign)
      .optional()
      .default(BlocksEnums.block.FullWidthCampaign),
  })
  .merge(fullWidthCampaignSchema)

export const fullWidthCampaignBlockRefsSchema = z.object({
  full_width_campaign: z.object({
    full_width_campaignConnection: z.object({
      edges: z.array(
        z.object({
          node: z.discriminatedUnion("__typename", [
            pageLinks.accountPageRefSchema,
            pageLinks.contentPageRefSchema,
            pageLinks.loyaltyPageRefSchema,
            pageLinks.collectionPageRefSchema,
            pageLinks.hotelPageRefSchema,
            pageLinks.destinationCityPageRefSchema,
            pageLinks.destinationCountryPageRefSchema,
            pageLinks.destinationOverviewPageRefSchema,
          ]),
        })
      ),
    }),
  }),
})
