/**
 * GROQ queries for the editorial surfaces (/news and /studies).
 *
 * All queries exclude drafts via `!(_id in path("drafts.**"))` so the
 * published-only perspective is enforced at the query level as well as
 * the client level — belt and braces.
 *
 * List queries are paginated via `$start`/`$end` slice parameters so the
 * caller controls page size without string interpolation.
 */

export const POSTS_QUERY = `*[_type == "post" && !(_id in path("drafts.**"))] | order(featured desc, publishedAt desc) [$start...$end] {
  _id, title, "slug": slug.current, excerpt, coverImage, publishedAt, featured,
  "author": author->{name, "slug": slug.current}
}`;

export const POST_BY_SLUG_QUERY = `*[_type == "post" && slug.current == $slug && !(_id in path("drafts.**"))][0]{
  _id, title, "slug": slug.current, excerpt, coverImage, publishedAt, body,
  "author": author->{name, "slug": slug.current, bio, avatar, institution},
  "categories": categories[]->{ title, "slug": slug.current }
}`;

export const POST_SLUGS_QUERY = `*[_type == "post" && defined(slug.current) && !(_id in path("drafts.**"))][]{ "slug": slug.current }`;

export const STUDIES_QUERY = `*[_type == "study" && !(_id in path("drafts.**"))] | order(publishedAt desc) [$start...$end] {
  _id, title, "slug": slug.current, summary, coverImage, publishedAt, institutions,
  "authors": authors[]->{name, "slug": slug.current}
}`;

export const STUDY_BY_SLUG_QUERY = `*[_type == "study" && slug.current == $slug && !(_id in path("drafts.**"))][0]{
  _id, title, "slug": slug.current, summary, coverImage, publishedAt, body,
  institutions, doiUrl,
  "authors": authors[]->{name, "slug": slug.current, bio, avatar, institution}
}`;

export const STUDY_SLUGS_QUERY = `*[_type == "study" && defined(slug.current) && !(_id in path("drafts.**"))][]{ "slug": slug.current }`;

export const AUTHOR_BY_SLUG_QUERY = `*[_type == "author" && slug.current == $slug][0]{
  _id, name, "slug": slug.current, bio, avatar, orcidUrl, institution,
  "posts": *[_type == "post" && author._ref == ^._id && !(_id in path("drafts.**"))] | order(publishedAt desc){
    _id, title, "slug": slug.current, excerpt, publishedAt, coverImage, featured
  },
  "studies": *[_type == "study" && ^._id in authors[]._ref && !(_id in path("drafts.**"))] | order(publishedAt desc){
    _id, title, "slug": slug.current, summary, publishedAt, coverImage, institutions,
    "authors": authors[]->{name, "slug": slug.current}
  }
}`;

export const AUTHOR_SLUGS_QUERY = `*[_type == "author" && defined(slug.current)][]{ "slug": slug.current }`;

// ---- Result types -----------------------------------------------------------

export interface PostListItem {
  _id: string;
  title: string;
  slug: string;
  excerpt: string;
  coverImage?: unknown;
  publishedAt: string;
  featured?: boolean;
  author?: { name: string; slug: string } | null;
}

export interface PostDetail extends PostListItem {
  body?: unknown;
  author?: {
    name: string;
    slug: string;
    bio?: string;
    avatar?: unknown;
    institution?: string;
  } | null;
  categories?: Array<{ title: string; slug: string }>;
}

export interface StudyListItem {
  _id: string;
  title: string;
  slug: string;
  summary: string;
  coverImage?: unknown;
  publishedAt: string;
  institutions?: string[];
  authors: Array<{ name: string; slug: string }>;
}

export interface StudyDetail extends StudyListItem {
  body?: unknown;
  doiUrl?: string;
  authors: Array<{
    name: string;
    slug: string;
    bio?: string;
    avatar?: unknown;
    institution?: string;
  }>;
}

export interface AuthorDetail {
  _id: string;
  name: string;
  slug: string;
  bio?: string;
  avatar?: unknown;
  orcidUrl?: string;
  institution?: string;
  posts: PostListItem[];
  studies: StudyListItem[];
}
