#!/usr/bin/env nu

# A command to generate an agent prompt to diagnose and formulate
# a plan for resolving a GitHub issue for pg_ai_query.
#
# IMPORTANT: This command is prompted to NOT write any code and to ONLY
# produce a plan. You should still be vigilant when running this but that
# is the expected behavior.
#
# The `<issue>` parameter can be either an issue number or a full GitHub
# issue URL.
def main [
  issue: any, # pg_ai_query issue number or URL
  --repo: string = "benodiwal/pg_ai_query" # GitHub repository in the format "owner/repo"
] {
  # TODO: This whole script doesn't handle errors very well. I actually
  # don't know Nu well enough to know the proper way to handle it all.

  let issueData = gh issue view $issue --json author,title,number,body,comments | from json
  let comments = $issueData.comments | each { |comment|
    $"
### Comment by ($comment.author.login)
($comment.body)
" | str trim
  } | str join "\n\n"

  $"
Deep-dive on this GitHub issue for pg_ai_query. Find the problem and generate a plan.
Do not write code. Explain the problem clearly and propose a comprehensive plan
to solve it.

# ($issueData.title) \(($issueData.number)\)

## Description
($issueData.body)

## Comments
($comments)

## Your Tasks

You are an experienced PostgreSQL and AI integration developer tasked with diagnosing issues.

1. Review the issue context and details.
2. Examine the relevant parts of the codebase. Analyze the code thoroughly
   until you have a solid understanding of how it works.
3. Explain the issue in detail, including the problem and its root cause.
4. Create a comprehensive plan to solve the issue. The plan should include:
   - Required code changes (SQL, C, or other relevant languages)
   - Potential impacts on PostgreSQL extension functionality
   - Database compatibility considerations
   - Necessary tests to be written or updated
   - Documentation updates
   - Performance considerations (especially for database operations)
   - Security implications (SQL injection, permissions, etc.)
   - Backwards compatibility with existing PostgreSQL versions
   - AI model integration considerations (if applicable)
   - Include the reference link to the source issue and any related discussions
5. Think deeply about all aspects of the task. Consider edge cases, potential
   challenges, and best practices for PostgreSQL extensions and AI integrations.
   Review the plan with the oracle and adjust it based on its feedback.

**ONLY CREATE A PLAN. DO NOT WRITE ANY CODE.** Your task is to create
a thorough, comprehensive strategy for understanding and resolving the issue.
" | str trim
}
