package agenthelp

type HelpPayload struct {
	OK                 bool              `json:"ok"`
	Tool               string            `json:"tool"`
	Audience           string            `json:"audience"`
	Kind               string            `json:"kind"`
	CommandGroup       string            `json:"command_group,omitempty"`
	AgentPromptCommand string            `json:"agent_prompt_command"`
	Docs               []string          `json:"docs,omitempty"`
	Workflow           []string          `json:"workflow"`
	Commands           map[string]string `json:"commands"`
}

type PromptPayload struct {
	OK       bool   `json:"ok"`
	Tool     string `json:"tool"`
	Audience string `json:"audience"`
	Kind     string `json:"kind"`
	Prompt   string `json:"prompt"`
}

func RootHelp() HelpPayload {
	return HelpPayload{
		OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
		AgentPromptCommand: "aget prompt",
		Docs:               []string{"AGENT_INSTRUCTIONS.md", "README.md"},
		Workflow: []string{
			"Use browser status first if you need to verify the managed CloakBrowser backend",
			"Open a URL with aget open and keep the returned sid",
			"Continue with returned sid and next_commands",
			"Use page snapshot after opening to get stable refs for actions",
			"Prefer page refs before CSS selectors when possible",
			"Use page read or page get for text extraction before deciding actions",
			"Use page screenshot when visual state matters",
			"Use batch for multi-step page workflows",
			"Run doctor when install or browser startup fails",
			"Always close sessions with aget session close when finished",
		},
		Commands: map[string]string{
			"browser_status":      "aget browser status",
			"browser_install":     "aget browser install",
			"doctor":              "aget doctor",
			"open":                "aget open URL -n NAME",
			"open_device":         "aget open URL --device mobile",
			"open_profile":        "aget open URL --profile NAME",
			"profile_create":      "aget profile create NAME --cookies COOKIES",
			"profile_list":        "aget profile list",
			"profile_show":        "aget profile show NAME",
			"profile_delete":      "aget profile delete NAME",
			"batch":               "aget batch -s SID --stdin",
			"page_snapshot":       "aget page snapshot -s SID",
			"page_read":           "aget page read -s SID --limit 80",
			"page_click":          "aget page click -s SID --selector CSS",
			"page_type":           "aget page type -s SID --selector CSS --text TEXT",
			"page_fill":           "aget page fill -s SID --ref REF --text TEXT",
			"page_select":         "aget page select -s SID --ref REF --value VALUE",
			"page_press":          "aget page press -s SID --key Enter",
			"page_wait":           "aget page wait -s SID --text TEXT",
			"page_wait_ref":       "aget page wait -s SID --ref REF",
			"page_get":            "aget page get -s SID text --ref REF",
			"page_is":             "aget page is -s SID --ref REF visible",
			"page_js":             "aget page js -s SID --expr EXPR",
			"page_check":          "aget page check -s SID --ref REF",
			"page_uncheck":        "aget page uncheck -s SID --ref REF",
			"page_hover":          "aget page hover -s SID --ref REF",
			"page_focus":          "aget page focus -s SID --ref REF",
			"page_upload":         "aget page upload -s SID --ref REF --file PATH",
			"page_dialog_accept":  "aget page dialog-accept -s SID",
			"page_dialog_dismiss": "aget page dialog-dismiss -s SID",
			"page_screenshot":     "aget page screenshot -s SID --path /tmp/page.png",
			"session_list":        "aget session list",
			"session_close":       "aget session close -s SID",
			"prompt":              "aget prompt",
		},
	}
}

func GroupHelp(name string) (HelpPayload, bool) {
	groups := map[string]HelpPayload{
		"page": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup: "page", AgentPromptCommand: "aget prompt",
			Workflow: []string{
				"Use page snapshot before actions to discover refs like @e1 and @i1",
				"Use ref actions from the snapshot before CSS selectors when possible",
				"Use snapshot -> ref action -> is/get/screenshot to verify results",
				"Use select for dropdowns, check/uncheck for checkboxes and radios",
				"Use js as a universal fallback for elements not covered by other commands",
				"Use screenshot when text output is insufficient",
			},
			Commands: map[string]string{
				"read":            "aget page read -s SID --limit 80",
				"click":           "aget page click -s SID --selector CSS",
				"click_ref":       "aget page click -s SID --ref REF",
				"type":            "aget page type -s SID --selector CSS --text TEXT",
				"type_ref":        "aget page type -s SID --ref REF --text TEXT",
				"snapshot":        "aget page snapshot -s SID",
				"fill":            "aget page fill -s SID --ref REF --text TEXT",
				"fill_selector":   "aget page fill -s SID --selector CSS --text TEXT",
				"select":          "aget page select -s SID --ref REF --value VALUE",
				"select_selector": "aget page select -s SID --selector CSS --value VALUE",
				"press":           "aget page press -s SID --key Enter",
				"wait":            "aget page wait -s SID --text TEXT",
				"wait_ref":        "aget page wait -s SID --ref REF",
				"scroll":          "aget page scroll -s SID --direction down --px 800",
				"get":             "aget page get -s SID text --ref REF",
				"is":              "aget page is -s SID --ref REF visible",
				"js":              "aget page js -s SID --expr EXPR",
				"check":           "aget page check -s SID --ref REF",
				"uncheck":         "aget page uncheck -s SID --ref REF",
				"hover":           "aget page hover -s SID --ref REF",
				"focus":           "aget page focus -s SID --ref REF",
				"upload":          "aget page upload -s SID --ref REF --file PATH",
				"dialog_accept":   "aget page dialog-accept -s SID",
				"dialog_dismiss":  "aget page dialog-dismiss -s SID",
				"screenshot":      "aget page screenshot -s SID --path /tmp/page.png",
			},
		},
		"browser": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup: "browser", AgentPromptCommand: "aget prompt",
			Workflow: []string{
				"Use browser status to inspect the managed browser cache without network access",
				"Use browser install to download the pinned managed CloakBrowser stealth Chromium",
				"Use browser path to get the managed browser executable path",
			},
			Commands: map[string]string{
				"status":  "aget browser status",
				"install": "aget browser install",
				"path":    "aget browser path",
			},
		},
		"batch": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup: "batch", AgentPromptCommand: "aget prompt",
			Workflow: []string{
				"Use batch for multi-step page workflows after opening a session",
				"Read one JSON array from stdin with --stdin",
				"Batch stops at the first failed step and returns JSON on stdout",
				"Do not include secrets in logs; fill results report text_len only",
			},
			Commands: map[string]string{
				"batch": "aget batch -s SID --stdin",
			},
		},
		"doctor": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup: "doctor", AgentPromptCommand: "aget prompt",
			Workflow: []string{
				"Run doctor when install, browser resolution, or startup readiness is unclear",
				"Doctor emits JSON checks and exits non-zero if any check fails",
				"Doctor is non-destructive and does not download browsers",
			},
			Commands: map[string]string{
				"doctor": "aget doctor",
			},
		},
		"session": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup: "session", AgentPromptCommand: "aget prompt",
			Workflow: []string{
				"Use returned sid values to continue browser workflows",
				"List sessions when you need to recover active sid values",
				"Always close sessions when finished",
			},
			Commands: map[string]string{
				"list":  "aget session list",
				"close": "aget session close -s SID",
				"gc":    "aget session gc",
			},
		},
		"profile": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup: "profile", AgentPromptCommand: "aget prompt",
			Workflow: []string{
				"Use profile create to set up a persistent browser profile with cookies once",
				"Use profile list to discover available profiles",
				"Use open --profile to reuse a profile and keep logged-in state across sessions",
				"Use profile delete to remove a profile and all its data",
			},
			Commands: map[string]string{
				"create":    "aget profile create NAME --cookies COOKIES",
				"list":      "aget profile list",
				"show":      "aget profile show NAME",
				"delete":    "aget profile delete NAME",
				"open_with": "aget open URL --profile NAME",
			},
		},
		"open": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup: "open", AgentPromptCommand: "aget prompt",
			Workflow: []string{
				"Open a URL and keep the returned sid",
				"Use --device mobile|tablet|desktop for device emulation (coherent viewport + UA + touch)",
				"Use --profile to reuse a persistent profile with cookies",
				"Follow next_commands from the response",
			},
			Commands: map[string]string{
				"open":         "aget open URL -n NAME",
				"open_device":  "aget open URL --device mobile",
				"open_profile": "aget open URL --profile NAME",
				"open_cookies": "aget open URL --cookies FILE",
			},
		},
		"version": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup:       "version",
			AgentPromptCommand: "aget prompt",
			Workflow:           []string{"Use version for diagnostics only"},
			Commands:           map[string]string{"version": "aget version"},
		},
		"prompt": {
			OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_help",
			CommandGroup:       "prompt",
			AgentPromptCommand: "aget prompt",
			Workflow:           []string{"Load this prompt when an LLM agent needs usage instructions"},
			Commands: map[string]string{
				"prompt":             "aget prompt",
				"agent_instructions": "aget agent-instructions",
			},
		},
	}
	payload, ok := groups[name]
	return payload, ok
}

func Prompt() PromptPayload {
	return PromptPayload{
		OK: true, Tool: "aget", Audience: "llm_agent", Kind: "agent_prompt",
		Prompt: "You are using aget, a browser workflow CLI for LLM agents backed by managed CloakBrowser stealth Chromium when available. All operational commands return JSON. Use `aget browser status` to inspect the managed browser when needed and `aget doctor` when install or browser startup fails. For persistent logged-in state, use profiles: `aget profile create NAME --cookies FILE` once, then `aget open URL --profile NAME` to reuse cookies across sessions. Use `aget profile list` to discover available profiles. For mobile or tablet pages, use `aget open URL --device mobile` or `--device tablet` (applies coherent viewport, user-agent, and touch emulation for stealth). Start with `aget open URL`, save the returned `sid`, then prefer `aget page snapshot -s SID` before actions because it returns refs like `@e1` and `@i1`. Use refs before CSS selectors when possible: `aget page click -s SID --ref REF`, `aget page fill -s SID --ref REF --text TEXT`, `aget page select -s SID --ref REF --value VALUE`. For checkboxes/radios use `aget page check -s SID --ref REF`. Verify state with `aget page is -s SID --ref REF visible|checked|enabled|focused`. Fall back with `aget page js -s SID --expr EXPR` for anything not covered. Use `aget page read -s SID --limit 80`, `aget page get -s SID text --ref REF`, `aget page wait -s SID --text TEXT`, `aget page scroll -s SID --direction down --px 800`, `aget page screenshot -s SID --path /tmp/page.png`, and `aget batch -s SID --stdin` for multi-step workflows. Avoid echoing sensitive text from forms, cookies, tokens, or private pages. Continue with returned `next_commands` and always run `aget session close -s SID` when finished.",
	}
}
