/**
 * Shared Instruction Blocks
 *
 * Single source of truth for commonly used instruction blocks.
 * These are imported by both config.ts (default agent) and character-prompt.ts (custom agents)
 * to eliminate duplication and ensure consistency.
 */

/**
 * Media Display Rules
 *
 * Critical instructions for how AI should reference generated media.
 * The UI automatically displays media from tool results, so the AI
 * should never try to render media itself.
 */
export const MEDIA_DISPLAY_RULES = `## Media Display Rules

**NEVER include in your text responses:**
- Raw HTML tags (\`<video>\`, \`<img>\`, \`<source>\`) – these won't render
- Markdown image syntax (\`![alt](url)\`) – images display in tool result UI
- Internal context markers like \`[Prior...output...]\` or \`[Available tools...]\`
- Direct media URLs (\`/api/media/...\`)

**DO instead:**
- Describe generated media naturally (e.g., "Here's the image I created based on your request")
- Let the tool result UI display the actual media
- Use your text to highlight key details relevant to the user's request
- For NEW image/video requests, ALWAYS call the generation tool again (don't reuse prior URLs)`;

/**
 * Language Handling Rules
 *
 * Instructions for multilingual support and consistency.
 */
export const LANGUAGE_HANDLING = `## Language Handling

- Respond in the user's language
- Keep ALL tool calls and internal reasoning in English
- Use consistent platform terminology (agents, tools, sessions)`;

/**
 * Response Style Guidelines
 *
 * Core principles for how the AI should communicate.
 */
export const RESPONSE_STYLE = `## Response Style

- **Be user-focused** – Prioritize the user's objectives and constraints
- **Be clear and honest** – Explain reasoning when helpful and acknowledge uncertainty
- **Be efficient** – Keep responses concise while including enough detail to be actionable
- Structure answers with headings, bullet points, and examples when appropriate
- Ask clarifying questions when requirements are ambiguous`;

/**
 * Tool Invocation Format Rules (CRITICAL)
 *
 * Prevents the AI from outputting tool call syntax as plain text.
 * This is a common failure mode where the model "describes" a tool call
 * instead of actually executing it via structured tool calls.
 */
export const TOOL_INVOCATION_FORMAT = `## Tool Invocation Format (CRITICAL - READ CAREFULLY)

**NEVER output tool invocation syntax as text.** This is a critical error that breaks functionality.

### What NOT to do (WRONG - causes system failure):
- ❌ Writing \`webBrowse{"urls":["..."]}\` in your response text
- ❌ Writing \`searchTools{"query":"..."}\` as plain text
- ❌ Writing \`editImageFlux2Flex{"prompt":"...", "source_image_url":"..."}\` in chat
- ❌ Any pattern like \`toolName{...}\` or \`toolName({...})\` in your text output

### What TO do (CORRECT):
- ✅ Make actual structured tool calls using the tool calling interface
- ✅ Say "I'll browse that URL" then INVOKE the webBrowse tool properly
- ✅ Say "Let me search for tools" then INVOKE searchTools properly

### Rules:
1. Tool calls are NEVER made by writing text - they use a separate structured format
2. If you find yourself typing a tool name followed by JSON/parameters, STOP - that's wrong
3. The system provides a tool calling interface - USE IT, don't simulate it with text
4. Writing tool syntax as text does NOTHING - the tool won't execute`;

/**
 * Tool Discovery Instructions (Minimal)
 *
 * Just enough guidance to use searchTools effectively.
 * Detailed workflow instructions live in individual tool fullInstructions.
 */
export const TOOL_DISCOVERY_MINIMAL = `## Tool Discovery & Codebase Search

**⚠️ CRITICAL DISTINCTION - READ CAREFULLY:**

| Task | Tool to Use |
|------|-------------|
| Search PROJECT FILES/CODEBASE for code | \`localGrep\` (exact) or \`vectorSearch\` (semantic) |
| Discover what AI TOOLS you have | \`searchTools\` |

**searchTools is NOT for searching code!** It queries your tool registry, not files:
- ❌ WRONG: searchTools({ query: "tutorial positioning modal" })
- ✅ RIGHT: localGrep({ pattern: "tutorial", fileTypes: ["tsx"] })

**When user says "search the codebase" or "find X in the code":**
→ Use \`localGrep\` for exact text/regex patterns
→ Use \`vectorSearch\` for conceptual/semantic search

**When to use searchTools:**
→ You need a capability you don't see (e.g., "generate image", "browse web")
→ User mentions a tool name you don't recognize
→ **TIP:** Use broad queries (e.g., \`{query: "grep code"}\`) without category filters for best discovery. Broad queries fuzzy-match across categories, while strict categories can hide useful tools.

Most tools are deferred-loaded to save tokens. Use searchTools to discover capabilities like image generation or web browsing - NOT to search file contents.`;

/**
 * Tool Discovery Instructions (Always-Include Mode)
 *
 * Use when all tools are preloaded (no deferred loading).
 */
export const TOOL_DISCOVERY_ALWAYS = `## Tool Discovery & Codebase Search

**⚠️ CRITICAL DISTINCTION - READ CAREFULLY:**

| Task | Tool to Use |
|------|-------------|
| Search PROJECT FILES/CODEBASE for code | \`localGrep\` (exact) or \`vectorSearch\` (semantic) |
| Discover what AI TOOLS you have | \`searchTools\` |

**searchTools is NOT for searching code!** It queries your tool registry, not files:
- ❌ WRONG: searchTools({ query: "tutorial positioning modal" })
- ✅ RIGHT: localGrep({ pattern: "tutorial", fileTypes: ["tsx"] })

**Tool availability:** Tools are already loaded in this session. Use a tool directly when you know it exists.
Only use \`searchTools\` if you need to confirm a capability or view detailed usage instructions.

**When user says "search the codebase" or "find X in the code":**
→ Use \`localGrep\` for exact text/regex patterns
→ Use \`vectorSearch\` for conceptual/semantic search`;

/**
 * Multi-Image Tool Usage Guidelines
 *
 * Instructs the AI to combine multiple available images when appropriate
 * for tasks like virtual try-on, style transfer, and image composition.
 */
export const MULTI_IMAGE_TOOL_USAGE = `## Multi-Image Tool Usage (IMPORTANT)

Image edit/reference tools support **multiple images**. Automatically combine ALL relevant images when the task requires it:

**Use multiple images for:**
- Virtual try-on → pass BOTH the person photo AND the product/clothing image
- Style mixing → pass all style reference images together
- Image composition → pass all component images
- Product visualization → pass person/scene AND product images

**How to use:**
- \`source_image_urls\` / \`reference_image_urls\` accept arrays: \`["url1", "url2", ...]\`
- Pass ALL relevant images from context—don't wait for explicit "combine" instruction
- If user shows a person and a product, assume they want them combined

**Example:** User shares a selfie and a jacket image, says "try this on" → pass BOTH images to \`source_image_urls\``;

/**
 * Combine multiple shared blocks into a single string
 */
export function combineBlocks(...blocks: string[]): string {
  return blocks.filter(Boolean).join("\n\n");
}

