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

export default [
  defineTask({
    id: "content-lake-mutations",
    mode: "literacy",
    title: "Content Lake - CRUD operations with @sanity/client",
    description: "Content Lake - CRUD operations with @sanity/client",
    // Used for score aggregation in reports and --area CLI filtering
    area: "content-lake",
    // Auto-generates an additional rubric checking if the response uses the provided docs
    docCoverage: true,
    context: {
      // Sanity doc pages fetched and injected into the prompt as context; slug resolves via content lake
      docs: [
        {
          slug: "js-client-mutations",
          reason: "Creating and updating documents with @sanity/client",
        },
        {
          slug: "js-client-transactions",
          reason: "Atomic transactions — multiple mutations in one request",
        },
        {
          slug: "js-client-deleting",
          reason: "Deleting documents with @sanity/client",
        },
        {
          slug: "http-patches",
          reason: "Patch operations — set, unset, inc, insert, diffMatchPatch",
        },
      ],
    },
    // Path (relative to eval package root) to a gold-standard implementation the grader compares against
    referenceSolution: "reference-solutions/content-lake/mutations.ts",
    // The instruction the model under evaluation sees. The compiler auto-derives
    // the docs context (file://contexts/canonical/{task.id}.md) from context.docs.
    prompt: {
      text: `Implement a content management service using @sanity/client that
performs CRUD operations on Sanity documents.

Requirements:
1. Create a new document with typed fields
2. Patch an existing document (set fields, unset fields, increment a counter)
3. Delete a document by ID
4. Use a transaction to atomically create multiple documents
5. Include proper TypeScript types

Provide a complete, reusable implementation.`,
    },
    // Grading criteria applied to the model's response; each assertion produces a pass/fail contributing to the task score
    assertions: [
      // llm-rubric with template: the grader model evaluates the response against these criteria
      {
        type: "llm-rubric",
        template: "task-completion",
        criteria: [
          "Document creation with client.create() or client.createOrReplace()",
          "Patch operations with set/unset/inc",
          "Document deletion with client.delete()",
          "Transaction with multiple mutations",
        ],
      },
      {
        type: "llm-rubric",
        template: "code-correctness",
        criteria: [
          "Correct @sanity/client API usage",
          "Proper patch chaining syntax",
          "Transaction commit pattern",
          "TypeScript types for document shapes",
        ],
      },
      // Deterministic string-match assertions — fast, cheap, no grader model needed
      { type: "contains", value: "createClient", weight: 1 },
      // contains-any: passes if ANY string in the array appears in the response
      {
        type: "contains-any",
        value: [".create(", ".createOrReplace(", ".createIfNotExists("],
        weight: 1,
      },
      {
        type: "contains-any",
        value: [".patch(", ".set("],
        weight: 1,
      },
      {
        type: "contains-any",
        value: [".transaction(", "transaction"],
        weight: 1,
      },
      {
        type: "contains-any",
        value: [".delete(", "client.delete"],
        weight: 1,
      },
    ],
  }),

  defineTask({
    id: "content-lake-realtime",
    mode: "literacy",
    title: "Content Lake - Real-time listeners with @sanity/client",
    description: "Content Lake - Real-time listeners with @sanity/client",
    area: "content-lake",
    docCoverage: true,
    context: {
      docs: [
        {
          slug: "js-client-realtime",
          reason: "Listening to content updates with @sanity/client",
        },
        {
          slug: "realtime-updates",
          reason: "Real-time updates — listener protocol, event types",
        },
        {
          slug: "js-client-getting-started",
          reason: "Client setup — configuration, tokens, API version",
        },
        {
          slug: "js-client-querying",
          reason:
            "Querying content — needed for initial data fetch before listening",
        },
      ],
    },
    referenceSolution: "reference-solutions/content-lake/realtime.ts",
    prompt: {
      text: `Implement real-time content synchronization using Sanity's listener API.

Requirements:
1. Set up a listener that watches for changes to documents of a specific type
2. Handle mutation events (create, update, delete)
3. Apply changes to a local in-memory cache
4. Handle reconnection and error scenarios
5. Provide a way to unsubscribe/clean up the listener

Provide a complete implementation with TypeScript types.`,
    },
    assertions: [
      {
        type: "llm-rubric",
        template: "task-completion",
        criteria: [
          "Listener setup with GROQ filter for document type",
          "Mutation event handling (create, update, delete differentiation)",
          "Local cache synchronization",
          "Cleanup/unsubscribe mechanism",
          "Error or reconnection handling",
        ],
      },
      {
        type: "llm-rubric",
        template: "code-correctness",
        criteria: [
          "Correct client.listen() API usage",
          "Proper GROQ filter syntax in listener",
          "Subscription cleanup pattern (unsubscribe)",
          "No deprecated listener APIs",
        ],
      },
      {
        type: "contains-any",
        value: [".listen(", "client.listen"],
        weight: 1,
      },
      {
        type: "contains-any",
        value: ["subscribe", ".subscribe("],
        weight: 1,
      },
    ],
  }),
]
