/**
 * Reference Solution: GROQ Blog Queries
 *
 * Demonstrates:
 * - Filtering documents by type
 * - Projections to shape query output
 * - Ordering and pagination with slicing
 * - Parameterized queries
 * - Date filtering
 * - Reference-based filtering
 */

import { createClient } from "@sanity/client"

export const client = createClient({
  projectId: "your-project-id",
  dataset: "production",
  apiVersion: "2024-01-01",
  useCdn: process.env.NODE_ENV === "production",
})

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

interface PostSummary {
  _id: string
  title: string
  slug: string
  publishedAt: string
  excerpt: string
  authorName: string
}

interface PostDetail extends PostSummary {
  body: unknown[] // Portable Text blocks
}

// ---------------------------------------------------------------------------
// 1. Fetch paginated blog posts — filter, project, order & slice
// ---------------------------------------------------------------------------

const paginatedPostsQuery = /* groq */ `
  *[_type == "post"] | order(publishedAt desc) [0...10] {
    _id,
    title,
    "slug": slug.current,
    publishedAt,
    excerpt,
    "authorName": author->name
  }
`

export async function getPaginatedPosts(
  start = 0,
  end = 10,
): Promise<PostSummary[]> {
  return client.fetch(
    /* groq */ `
      *[_type == "post"] | order(publishedAt desc) [$start...$end] {
        _id,
        title,
        "slug": slug.current,
        publishedAt,
        excerpt,
        "authorName": author->name
      }
    `,
    { start, end },
  )
}

// ---------------------------------------------------------------------------
// 2. Fetch a single post by slug — parameterized query
// ---------------------------------------------------------------------------

const postBySlugQuery = /* groq */ `
  *[_type == "post" && slug.current == $slug][0] {
    _id,
    title,
    "slug": slug.current,
    publishedAt,
    excerpt,
    "authorName": author->name,
    body
  }
`

export async function getPostBySlug(
  slug: string,
): Promise<PostDetail | null> {
  return client.fetch(postBySlugQuery, { slug })
}

// ---------------------------------------------------------------------------
// 3. Fetch posts published after a specific date — dateTime() filtering
// ---------------------------------------------------------------------------

const recentPostsQuery = /* groq */ `
  *[_type == "post" && dateTime(publishedAt) > dateTime($since)]
    | order(publishedAt desc) {
      _id,
      title,
      "slug": slug.current,
      publishedAt,
      excerpt,
      "authorName": author->name
    }
`

export async function getPostsPublishedAfter(
  since: string,
): Promise<PostSummary[]> {
  return client.fetch(recentPostsQuery, { since })
}

// ---------------------------------------------------------------------------
// 4. Fetch posts by category — reference check in an array
// ---------------------------------------------------------------------------

const postsByCategoryQuery = /* groq */ `
  *[_type == "post" && $categoryId in categories[]._ref]
    | order(publishedAt desc) {
      _id,
      title,
      "slug": slug.current,
      publishedAt,
      excerpt,
      "authorName": author->name
    }
`

export async function getPostsByCategory(
  categoryId: string,
): Promise<PostSummary[]> {
  return client.fetch(postsByCategoryQuery, { categoryId })
}
