import type { SavedPrompt, SmartPlacement } from "../stores/promptLibrary";

/** Human-readable descriptions for all auto-resolvable context variables.
 *  Used by VariableInputDialog and the dropdown tooltip. */
export const VARIABLE_DESCRIPTIONS: Record<string, string> = {
	// Git context (resolved by Rust backend)
	branch: "Current git branch name",
	base_branch: "Main/master/develop branch detected in the repo",
	diff: "Unstaged changes (git diff)",
	staged_diff: "Staged changes (git diff --staged)",
	changed_files: "List of modified files (git status --short)",
	commit_log: "Last 20 commits (one-line format)",
	last_commit: "Latest commit hash and message",
	conflict_files: "Files with unresolved merge conflicts",
	stash_list: "Current git stash entries",
	repo_name: "Repository directory name",
	repo_path: "Full filesystem path to the repository root",
	// GitHub/PR context (resolved from frontend stores)
	pr_number: "Pull request number for the current branch",
	pr_title: "Pull request title",
	pr_url: "GitHub pull request URL",
	pr_state: "PR state: OPEN, MERGED, or CLOSED",
	pr_checks: "CI check summary (e.g. '3 passed, 1 failed')",
	merge_status: "Merge status: MERGEABLE, CONFLICTING, or BEHIND",
	review_decision: "Review status: APPROVED, CHANGES_REQUESTED, or REVIEW_REQUIRED",
	// Agent/terminal context
	agent_type: "Active agent type (claude, aider, codex, etc.)",
	cwd: "Active terminal working directory",
	// Issue context (resolved from expanded issue in the sidebar)
	issue_number: "GitHub issue number",
	issue_title: "GitHub issue title",
	issue_author: "GitHub issue author",
	issue_labels: "Comma-separated issue labels",
	issue_state: "Issue state: OPEN or CLOSED",
	issue_url: "GitHub issue URL",
	issue_assignees: "Comma-separated assignee usernames",
	issue_milestone: "Issue milestone name",
	issue_comments_count: "Number of comments on the issue",
	// File context (populated by placement="file-context" hosts)
	file_path: "Absolute path of the selected file or folder",
	file_rel_path: "Path relative to the repository root",
	file_name: "Basename of the file (e.g. foo.ts)",
	file_ext: "File extension including the dot (e.g. .ts)",
	file_dir: "Parent directory absolute path",
	file_is_dir: "'true' if the target is a folder, else 'false'",
};

/** Shared defaults for all built-in smart prompts */
function builtin(
	id: string,
	name: string,
	description: string,
	icon: string,
	placement: SmartPlacement[],
	content: string,
	tags: string[],
	opts: Partial<SavedPrompt> = {},
): SavedPrompt {
	return {
		id,
		name,
		description,
		content,
		icon,
		placement,
		tags: ["smart", ...tags],
		category: "custom",
		isFavorite: false,
		builtIn: true,
		builtInVersion: 3,
		autoExecute: true,
		requiresIdle: true,
		executionMode: "inject",
		enabled: true,
		createdAt: 0,
		updatedAt: 0,
		...opts,
	};
}

export const SMART_PROMPTS_BUILTIN: SavedPrompt[] = [
	// ── Git & Commit ────────────────────────────────────────────────
	builtin(
		"smart-commit",
		"Smart Commit",
		"Stage and commit with an auto-generated conventional message",
		"git-commit",
		["toolbar", "git-changes"],
		"Look at the currently staged changes and create a commit with a good conventional commit message. If nothing is staged, stage the relevant changes first.",
		["git"],
	),
	builtin(
		"smart-commit-push",
		"Commit & Push",
		"Stage, commit, and push to the remote tracking branch",
		"git-push",
		["toolbar"],
		"Stage all changes, create a commit with a conventional commit message, and push to origin/{branch}.",
		["git"],
	),
	builtin(
		"smart-amend",
		"Amend Commit",
		"Add staged changes to the last commit and update its message",
		"git-amend",
		["git-changes"],
		"Amend the last commit with the currently staged changes. Update the commit message if the scope changed.",
		["git"],
	),
	builtin(
		"smart-commit-msg",
		"Generate Commit Message",
		"Generate a conventional commit message from current changes",
		"sparkle",
		["git-changes"],
		"You are a commit message generator. Output ONLY the raw commit message text — no explanation, no markdown, no code fences, no commentary. Use conventional commits format (type(scope): description). If there is a staged diff, base the message on staged changes only; otherwise use the full diff.\n\nChanged files:\n{changed_files}\n\nStaged diff:\n{staged_diff}\n\nFull diff:\n{diff}",
		["git"],
		{ executionMode: "headless", outputTarget: "commit-message" },
	),

	// ── Code Review ─────────────────────────────────────────────────
	builtin(
		"smart-review",
		"Review Changes",
		"Review all uncommitted changes for bugs, security, and quality issues",
		"magnifier",
		["toolbar", "git-changes"],
		"Review the uncommitted changes in this repository for bugs, security issues, performance problems, and code quality. Be specific with file names and line numbers.\n\nChanged files:\n{changed_files}",
		["review"],
	),
	builtin(
		"smart-review-staged",
		"Review Staged",
		"Review only staged changes before committing",
		"check-diff",
		["git-changes"],
		"Review only the staged changes for bugs, security issues, and code quality:\n\n{staged_diff}",
		["review"],
	),
	builtin(
		"smart-review-pr",
		"Review PR",
		"Full code review of the pull request on the current branch",
		"pr-review",
		["pr-popover"],
		'Review PR #{pr_number} "{pr_title}". Checkout the branch if needed, review all changes, and provide detailed feedback on code quality, bugs, security, and improvements.',
		["review"],
	),
	builtin(
		"smart-review-comments",
		"Address Review Comments",
		"Go through PR review comments and resolve each one",
		"chat",
		["pr-popover"],
		"Check the review comments on PR #{pr_number} and address each one. For each comment, either make the requested change or explain why you disagree.",
		["review"],
	),

	// ── Issues ──────────────────────────────────────────────────────
	builtin(
		"smart-review-issue",
		"Review Issue",
		"Analyze a GitHub issue: feasibility, complexity, and implementation approach",
		"issue-review",
		["issue-popover"],
		'Review GitHub issue #{issue_number} "{issue_title}" ({issue_url}).\n\nLabels: {issue_labels}\nAuthor: {issue_author}\nAssignees: {issue_assignees}\nMilestone: {issue_milestone}\nComments: {issue_comments_count}\n\nAnalyze this issue: assess feasibility, estimate complexity (S/M/L/XL), identify affected areas in the codebase, suggest an implementation approach, and flag any risks or open questions.',
		["review", "investigation"],
	),

	// ── Pull Requests ───────────────────────────────────────────────
	builtin(
		"smart-create-pr",
		"Create PR",
		"Create a GitHub pull request with auto-generated title and description",
		"pr-open",
		["toolbar", "git-branches"],
		"Create a GitHub pull request for branch {branch} targeting {base_branch}. Generate a descriptive title and comprehensive description based on the commits:\n\n{commit_log}\n\nUse `gh pr create`.",
		["pr"],
	),
	builtin(
		"smart-update-pr-desc",
		"Update PR Description",
		"Rewrite the PR description to match the current state of changes",
		"edit",
		["pr-popover"],
		"Update the description of PR #{pr_number} to accurately reflect the current state of changes. Use `gh pr edit`.",
		["pr"],
	),
	builtin(
		"smart-pr-description",
		"Generate PR Description",
		"Generate a PR title and description and copy to clipboard",
		"clipboard",
		["pr-popover"],
		"Generate a pull request title and description for branch {branch} targeting {base_branch}. Format as:\n\nTitle: <title>\n\n<description body>\n\nBased on commits:\n{commit_log}",
		["pr"],
		{ executionMode: "headless", outputTarget: "clipboard" },
	),

	// ── Merge & Conflicts ──────────────────────────────────────────
	builtin(
		"smart-resolve-conflicts",
		"Resolve Conflicts",
		"Analyze and resolve merge conflicts in all affected files",
		"merge",
		["toolbar", "pr-popover"],
		"Resolve the merge conflicts in this repository. The conflicting files are:\n{conflict_files}\n\nFor each file, analyze both sides of the conflict, choose the best resolution that preserves intent from both branches, and stage the resolved files.",
		["merge"],
	),
	builtin(
		"smart-merge-main",
		"Merge Main Into Branch",
		"Pull latest changes from the base branch and merge into the current branch",
		"git-merge",
		["toolbar", "git-branches"],
		"Merge the latest {base_branch} into {branch}. If there are conflicts, resolve them thoughtfully — favor the feature branch intent while incorporating upstream changes. Stage and commit the merge.",
		["merge"],
	),
	builtin(
		"smart-rebase-main",
		"Rebase on Main",
		"Rebase the current branch onto the latest base branch",
		"git-rebase",
		["git-branches"],
		"Rebase {branch} onto the latest {base_branch}. Resolve any conflicts that arise, then force-push if needed.",
		["merge"],
	),

	// ── CI & Quality ────────────────────────────────────────────────
	builtin(
		"smart-fix-ci",
		"Fix CI Failures",
		"Investigate and fix failing CI checks on the pull request",
		"ci-fix",
		["pr-popover"],
		"PR #{pr_number} has failing CI checks: {pr_checks}\n\nInvestigate the failures, identify the root cause, and fix them. Run the failing tests locally to verify before pushing.",
		["ci"],
	),
	builtin(
		"smart-fix-lint",
		"Fix Lint Issues",
		"Run linter/formatter and fix all style violations without changing logic",
		"lint",
		["toolbar"],
		"Run the linter/formatter for this project and fix all issues. Don't change logic — only fix formatting, style, and lint violations.",
		["ci"],
	),
	builtin(
		"smart-write-tests",
		"Write Tests",
		"Generate comprehensive tests for changed files with edge cases",
		"test",
		["toolbar", "git-changes"],
		"Write comprehensive tests for the changes in this branch. Focus on:\n\nChanged files:\n{changed_files}\n\nCover happy paths, edge cases, and error conditions. Follow the existing test patterns in the project.",
		["ci"],
	),
	builtin(
		"smart-run-tests",
		"Run & Fix Tests",
		"Run the test suite and automatically fix any failures",
		"test-run",
		["toolbar"],
		"Run the test suite. If any tests fail, investigate the root cause and fix them. Report what was wrong and what you fixed.",
		["ci"],
	),

	// ── Investigation & Context ─────────────────────────────────────
	builtin(
		"smart-investigate-issue",
		"Investigate Issue",
		"Deep-dive into a GitHub issue: read, explore code, find root cause",
		"search",
		["toolbar"],
		"Investigate GitHub issue #{issue_number}. Read the issue, understand the expected vs actual behavior, explore relevant code, identify the root cause, and propose a solution with specific file paths.",
		["investigation"],
	),
	builtin(
		"smart-what-changed",
		"What Changed?",
		"Summarize all changes on this branch since it diverged from base",
		"history",
		["toolbar"],
		"Summarize what changed on this branch since it diverged from {base_branch}. Show:\n- Overview of changes\n- Files modified and why\n- Any potential issues\n\nRecent commits:\n{commit_log}",
		["investigation"],
	),
	builtin(
		"smart-summarize-branch",
		"Summarize Branch",
		"Quick summary of what this branch does based on its commits",
		"branch-info",
		["toolbar", "git-branches"],
		"Summarize what branch {branch} does based on these commits:\n{commit_log}\n\nProvide a 2-3 sentence summary.",
		["investigation"],
		{ executionMode: "headless", outputTarget: "toast" },
	),
	builtin(
		"smart-explain-diff",
		"Explain Changes",
		"Explain uncommitted changes in plain language",
		"explain",
		["git-changes"],
		"Explain the current uncommitted changes in plain language. What do they do, and why might they have been made?\n\nChanged files:\n{changed_files}",
		["investigation"],
	),

	// ── Code Operations ─────────────────────────────────────────────
	builtin(
		"smart-refactor",
		"Suggest Refactoring",
		"Analyze recent changes and suggest refactoring opportunities",
		"refactor",
		["toolbar"],
		"Analyze the recent changes and suggest refactoring opportunities. Focus on reducing duplication, improving naming, and simplifying complex logic.\n\nChanged files:\n{changed_files}",
		["code"],
	),
	builtin(
		"smart-security",
		"Security Audit",
		"Check uncommitted changes for injection, credential exposure, and OWASP issues",
		"shield",
		["toolbar"],
		"Perform a security review of the uncommitted changes. Check for:\n- Injection vulnerabilities (SQL, command, XSS)\n- Credential exposure\n- Unsafe deserialization\n- Missing input validation\n- Dependency vulnerabilities\n\n{changed_files}",
		["code"],
	),

	// ── File Context ─────────────────────────────────────────────────
	builtin(
		"smart-file-explain",
		"Explain This File",
		"Summarize what this file does, its exports, and how it fits in the codebase",
		"explain",
		["file-context"],
		"Read {file_rel_path} and explain: (1) its primary responsibility in one sentence, (2) the public API / exports, (3) how it integrates with the rest of the codebase (who calls it, what it depends on). Be concise — no preamble.",
		["code", "investigation"],
	),
	builtin(
		"smart-file-review",
		"Review This File",
		"Targeted code review of a single file for bugs, quality, and security",
		"magnifier",
		["file-context"],
		"Review {file_rel_path} for bugs, security issues, performance problems, and code quality. Flag specific lines and suggest concrete fixes. Skip trivial style nits.",
		["review"],
	),
	builtin(
		"smart-file-tests",
		"Write Tests for This File",
		"Generate comprehensive tests for the selected file with edge cases",
		"test",
		["file-context"],
		"Write comprehensive tests for {file_rel_path}. Cover happy paths, edge cases, and error conditions. Follow the existing test patterns and framework already used in this project — detect them before writing.",
		["ci"],
	),
	builtin(
		"smart-file-refactor",
		"Suggest Refactoring",
		"Analyze this file and propose concrete refactoring opportunities",
		"refactor",
		["file-context"],
		"Analyze {file_rel_path} and suggest refactoring opportunities. Focus on reducing duplication, improving naming, extracting helpers, and simplifying complex logic. Be concrete — cite line numbers.",
		["code"],
	),
];
