import { gql } from 'graphql-request'
import fetchAPI from '@/lib/api/fetchAPI'
import { CONTACT_PAGE_FRAGMENT } from './fragments'
import { CONTACT_MENU_FRAGMENT } from './fragments'

/**
 * Query the contact page info and its associated contact menu
 */
export async function getContactPage(slug: string) {
  const data = await fetchAPI(
    gql`
      ${CONTACT_PAGE_FRAGMENT}
      ${CONTACT_MENU_FRAGMENT}
      query ContactPage($id: ID!) {
        page(id: $id, idType: URI) {
          ...ContactPageFragment
        }
        menu(id: "201", idType: DATABASE_ID) {
          ...ContactMenuFragment
        }
      }
    `,
    {
      variables: {
        id: slug,
      },
    }
  )

  return data
}

export async function getSalesRepPage() {
  const data = await fetchAPI(
    gql`
      ${CONTACT_PAGE_FRAGMENT}
      ${CONTACT_MENU_FRAGMENT}
      query SalesRepPage {
        page(id: "find-a-rep", idType: URI) {
          ...ContactPageFragment
        }
        menu(id: "201", idType: DATABASE_ID) {
          ...ContactMenuFragment
        }
        repStates(first: 100) {
          nodes {
            name
            salesRepresentatives {
              nodes {
                databaseId
                title
                featuredImage {
                  node {
                    alt: altText
                    imageUrl: sourceUrl
                    placeholder: sourceUrl(size: THUMBNAIL)
                  }
                }
                salesRepContent {
                  email
                  jobTitle
                  phoneNumber
                }
                productLines {
                  nodes {
                    name
                  }
                }
              }
            }
          }
        }
      }
    `
  )

  return data
}
