import { defineTask } from "@sanity/ailf-core"

// See content-lake.task.ts for detailed explanations of common task properties
export default [
  defineTask({
    id: "groq-blog-queries",
    mode: "literacy",
    title: "GROQ - Blog queries with filtering and pagination",
    description: "GROQ - Blog queries with filtering and pagination",
    area: "groq",
    docCoverage: true,
    context: {
      docs: [
        {
          slug: "groq-introduction",
          reason: "GROQ overview — syntax, filters, projections, core concepts",
        },
        {
          slug: "how-queries-work",
          reason: "Detailed walkthrough of GROQ query mechanics",
        },
        {
          slug: "query-cheat-sheet",
          reason: "Ready-made query patterns for common use cases",
        },
        {
          slug: "paginating-with-groq",
          reason: "Pagination patterns with slice syntax",
        },
      ],
    },
    referenceSolution: "reference-solutions/groq/blog-queries.ts",
    prompt: {
      text: `Write GROQ queries for a Sanity blog application:

1. Fetch all published blog posts ordered by publishedAt descending,
   with a projection that includes: _id, title, slug (from slug.current),
   publishedAt, excerpt, and the author's name (resolved from a reference)
2. Add pagination to return only the first 10 results
3. Fetch a single post by its slug parameter, including the full body
   content and resolved author and category references
4. Fetch posts published after a specific date
5. Fetch posts that belong to a specific category (where categories
   is an array of references)

Use @sanity/client with client.fetch() for all queries. Include
TypeScript types for the query results.`,
    },
    assertions: [
      {
        type: "llm-rubric",
        template: "task-completion",
        criteria: [
          'GROQ filter with _type == "post"',
          'Projection with aliased slug field ("slug": slug.current)',
          "Reference resolution with -> for author",
          "Ordering with | order(publishedAt desc)",
          "Slice/pagination syntax [0...10] or [0..9]",
          "Parameterized query with $slug for single post fetch",
          "Date filtering with dateTime() or string comparison",
          "Category filtering using references or array contains",
        ],
      },
      {
        type: "llm-rubric",
        template: "code-correctness",
        criteria: [
          "Valid GROQ syntax (proper filter brackets, projection braces)",
          "Uses @sanity/client createClient + client.fetch()",
          "Correct parameter passing syntax ($param)",
          "Proper reference dereference with ->",
          "No deprecated patterns",
        ],
      },
      {
        type: "contains-any",
        value: ["client.fetch", "createClient"],
      },
      {
        type: "contains-any",
        value: ["order(publishedAt", "order(_createdAt", "| order("],
      },
      {
        type: "contains-any",
        value: ["[0...10]", "[0..9]", "[0..."],
      },
    ],
  }),

  defineTask({
    id: "groq-joins-references",
    mode: "literacy",
    title: "GROQ - Joins and reference resolution",
    description: "GROQ - Joins and reference resolution",
    area: "groq",
    docCoverage: true,
    context: {
      docs: [
        {
          slug: "groq-joins",
          reason: "Join patterns — ->, []-> , subqueries, parent scope (^)",
        },
        {
          slug: "how-queries-work",
          reason: "Core query mechanics including reference traversal",
        },
        {
          slug: "groq-introduction",
          reason: "Overview including reference resolution basics",
        },
      ],
    },
    referenceSolution: "reference-solutions/groq/joins-references.ts",
    prompt: {
      text: `Write GROQ queries that demonstrate join patterns in Sanity:

1. Follow a single reference to resolve an author's full profile
   from a post (post.author -> author document with name, bio, image)
2. Resolve an array of category references from a post
   (post.categories[]-> with title and slug)
3. Write a reverse reference query: given an author's ID, find all
   posts by that author using a subquery and the parent scope operator (^)
4. Create a nested join: for each author, include their 5 most recent
   posts as a nested array
5. Use the references() function to find all documents that reference
   a specific document ID

Use @sanity/client with client.fetch(). Include TypeScript types.`,
    },
    assertions: [
      {
        type: "llm-rubric",
        template: "task-completion",
        criteria: [
          "Single reference follow with -> operator",
          "Array reference resolution with []->",
          "Reverse reference / subquery using *[references(^._id)]",
          "Nested join pattern with parent scope (^)",
          "The references() function",
        ],
      },
      {
        type: "llm-rubric",
        template: "code-correctness",
        criteria: [
          "Correct -> dereference syntax",
          "Valid []-> array dereference",
          "Proper use of ^ parent scope operator",
          "Valid references() function usage",
          "No made-up syntax",
        ],
      },
      { type: "contains", value: "->" },
      {
        type: "contains-any",
        value: ["references(", "references(^"],
      },
    ],
  }),

  defineTask({
    id: "groq-advanced-filtering",
    mode: "literacy",
    title: "GROQ - Advanced filtering and projections",
    description: "GROQ - Advanced filtering and projections",
    area: "groq",
    docCoverage: true,
    context: {
      docs: [
        {
          slug: "groq-functions",
          reason:
            "GROQ function reference — select(), coalesce(), count(), defined()",
        },
        {
          slug: "query-cheat-sheet",
          reason: "Common filtering patterns and match operator examples",
        },
        {
          slug: "groq-operators",
          reason: "Operator reference — match, comparison, logical operators",
        },
        {
          slug: "how-queries-work",
          reason: "Query mechanics including projections and ordering",
        },
      ],
    },
    referenceSolution: "reference-solutions/groq/advanced-filtering.ts",
    prompt: {
      text: `Write GROQ queries demonstrating advanced filtering and projection patterns:

1. Use select() for conditional projections — return different fields
   based on the document's _type (e.g., posts get excerpt, events get
   date and venue)
2. Use coalesce() for fallback values — e.g., use seoTitle if it
   exists, otherwise fall back to title
3. Use the match operator for full-text search in titles
4. Use count() to count documents matching a filter and to count
   items within an array field
5. Use defined() to filter for documents that have a specific field set
6. Filter items within an array using [condition] syntax
7. Order results by multiple fields (e.g., featured status first,
   then by publishedAt)

Use @sanity/client with client.fetch(). Include TypeScript types.`,
    },
    assertions: [
      {
        type: "llm-rubric",
        template: "task-completion",
        criteria: [
          "select() for conditional projections",
          "coalesce() for fallback values",
          "match operator for text search",
          "count() function usage",
          "defined() function for existence checks",
          "Array filtering with [condition]",
          "Multi-field ordering",
        ],
      },
      {
        type: "llm-rubric",
        template: "code-correctness",
        criteria: [
          "Valid select() syntax with => arrow notation",
          "Correct coalesce() usage",
          "Proper match operator usage (on text fields)",
          "Valid count() and defined() function calls",
          "Correct array filter syntax",
        ],
      },
      {
        type: "contains-any",
        value: ["select(", "coalesce("],
      },
      {
        type: "contains-any",
        value: ["count(", "defined("],
      },
      { type: "contains-any", value: ["match"] },
    ],
  }),
]
