import { groq } from "next-sanity";
import { client } from "../sanity";

export interface pillarPost {
  title: string;
  currentSlug: string;
  postImage: any;
  content: any;
  dateCreated: Date;
}

export async function getPillarPosts() {
  const query = groq`
        *[_type == 'pillar'] | order(_createdAt desc) {
        title,
            "currentSlug":slug.current,
            "postImage": image.asset._ref,
            content,
            "dateCreated": _createdAt,
        } 
    `;

  const data: pillarPost[] = await client.fetch(query, {}, { cache: "no-cache" });

  return data;
}

export async function getPillarPostBySlug(slug: string) {
  const query = `
        *[_type=="pillar" && slug.current == '${slug}']{
        title,
            "currentSlug": slug.current,
            "postImage": image.asset._ref,
            content,
            "dateCreated": _createdAt,
        }[0]
    `;
  const data: pillarPost = await client.fetch(query, {}, { cache: "no-cache" });
  return data;
}
