{# 
Template for the user prompt in SQL generation.

This template generates the user prompt for an AI assistant tasked with writing
SQL statements. It provides context about the database (objects, examples, facts)
and either asks for a new SQL statement or requests a fix for a previous statement
that had errors.

Expected context variables:
- prior_prompts: List of previous search prompts (if any)
- ctx: Context object with:
  - rendered_objects: Dictionary of rendered database objects
  - rendered_sql_examples: Dictionary of rendered SQL examples
  - rendered_facts: Dictionary of rendered facts
- prompt: The user's question or directive
- error: Optional error message from a previous SQL statement
- answer: The previous SQL statement that had errors (only used if error is defined)
#}
## Prior search prompts:

The context already contains the results from semantic searches using these prompts:
{% for prior_prompt in prior_prompts -%}
{{ loop.index }}. {{ prior_prompt }}
{% endfor %}

## Context:
The following context may be relevant to the user's directive/question.

{% if freq_used_objects|length > 0 %}
### Frequently used Objects:
Below is a list of frequently used database objects which may or may not be relevant.

<frequently-used-database-objects>
{% for obj in freq_used_objects -%}
{{ obj.id }}. {{ obj.objtype }}: {{ obj.name }}
{% endfor -%}
</frequently-used-database-objects>
{% endif %}

### Database Objects:
{% if ctx.rendered_objects|length > 0 %}
{% for obj in ctx.rendered_objects.values() %}
{{ obj }}
{% endfor %}
{% else %}
No database objects in current context.
{% endif %}

### SQL Examples:
{% if ctx.rendered_sql_examples|length > 0 %}
{% for ex in ctx.rendered_sql_examples.values() %}
{{ ex }}
{% endfor %}
{% else %}
No SQL examples in current context.
{% endif %}

### Facts:
{% if ctx.rendered_facts|length > 0 %}
{% for fact in ctx.rendered_facts.values() %}
{{ fact }}
{% endfor %}
{% else %}
No facts in current context.
{% endif %}

{% if error is none %}
## Task:
Author a valid SQL statement to address the following directive/question.
USER: "{{ prompt }}"
{% else %}
## Task:
A SQL statement was authored to address the user's directive/question, however it had errors.
Fix the SQL statement.

USER: "{{ prompt }}"

<sql-statement>
{{ answer }}
</sql-statement>

ERROR: {{ error }}
{% endif %}