import { z } from "zod"

import { linkUnionSchema, transformPageLink } from "../schemas/pageLinks"

const link = z.object({
  href: z.string(),
  title: z.string(),
})

export const validateSasTierComparisonSchema = z
  .object({
    all_sas_tier_comparison: z.object({
      items: z.array(
        z.object({
          scandic_column_title: z.string(),
          sas_column_title: z.string(),
          tier_matches: z.array(
            z.object({
              scandic_friends_tier_name: z.string(),
              sas_eb_tier_name: z.string(),
              title: z.string(),
              content: z.object({
                json: z.any(), // json
              }),
              link: link.optional(),
            })
          ),
          call_to_action: z.object({
            title: z.string().optional(),
            linkConnection: z.object({
              edges: z.array(
                z.object({
                  node: linkUnionSchema,
                })
              ),
            }),
          }),
        })
      ),
    }),
  })
  .transform((data) => {
    const { call_to_action, ...item } = data.all_sas_tier_comparison.items[0]

    const linkNode = call_to_action.linkConnection.edges[0]?.node
    const link = transformPageLink(linkNode)

    return { ...item, cta: { title: call_to_action.title, link } }
  })
