/**
 * Predefined One-Click Command: Recommend tests and generate and execute top recommended tests
 *
 * Spec order:
 *   skyramp_analyze_changes (combined analyze + discover + recommend)
 *   → Generate tests for top N recommended types
 *   → Execute each via skyramp_execute_test
 */
const fullRepoRecommendGenerateExecuteTopNSteps = [
    {
        stepIndex: 0,
        title: "Ensure workspace is initialized",
        description: "First check whether the repositoryPath is a git repository by verifying that a .git directory exists at the repo root. If there is no .git directory, do not attempt to initialize a Skyramp workspace and proceed without one. If .git exists, check whether .skyramp/workspace.yml exists. If workspace.yml exists, read it and capture language, framework, outputDir, and api.baseUrl for use in later steps. If workspace.yml does not exist and the user has not declined initialization, call skyramp_initialize_workspace with repositoryPath to create it, then read and capture language, framework, outputDir, and api.baseUrl.",
        toolCall: {
            toolName: "skyramp_initialize_workspace",
            description: "Initialize Skyramp workspace if not already present",
            inputs: {
                workspacePath: { source: "user", paramKey: "repositoryPath" },
            },
        },
        conditionalGuidance: "Follow server-level init rules: (1) Check for .git at repositoryPath — if missing, do not call skyramp_initialize_workspace and proceed without a workspace. (2) If .git exists, check for .skyramp/workspace.yml — if it exists, read it and do not call skyramp_initialize_workspace. (3) If workspace.yml does not exist, call skyramp_initialize_workspace only if the user has not explicitly declined. If the user declines, proceed without a workspace. Always capture language, framework, outputDir, and api.baseUrl for later steps.",
    },
    {
        stepIndex: 1,
        title: "Analyze changes and get recommendations",
        description: "Call skyramp_analyze_changes with repositoryPath and analysisScope from user. This single tool scans endpoints, discovers existing tests, and returns ranked test recommendations inline. Capture the stateFile path and the ranked recommendations for step 2.",
        toolCall: {
            toolName: "skyramp_analyze_changes",
            description: "Analyze repo and generate ranked recommendations inline; returns stateFile",
            inputs: {
                repositoryPath: { source: "user", paramKey: "repositoryPath" },
                scope: { source: "literal", value: "full_repo" },
            },
            outputs: ["stateFile", "recommendations"],
        },
        conditionalGuidance: "Always call skyramp_analyze_changes — do not skip this step or use a pre-existing stateFile. Capture stateFile and ranked recommendations for step 2.",
    },
    {
        stepIndex: 2,
        title: "Generate tests for top N recommended types",
        description: "From the ranked recommendations returned by skyramp_analyze_changes in step 1, take the top N recommended test types (high priority first, then medium; N = topN from user, default 3).\n\nIMPORTANT — UI or E2E in top N: If UI or E2E test types are among the top N, treat each recommendation independently. For each such recommendation, first search the repository for existing trace files suitable for the endpoint or path associated with that recommendation (look for .zip or .json Skyramp trace files in the repo that reference the recommended endpoint path or URL). If suitable traces are found, use them directly as inputs to skyramp_ui_test_generation (playwrightInput) or skyramp_e2e_test_generation (trace + playwrightInput) — skip trace collection. If no suitable traces exist in the repo for that recommendation, collect them: call skyramp_start_trace_collection (playwright: true, outputDir: absolute path from workspace.yml), have the user interact with the recommended endpoint/path in the browser, then call skyramp_stop_trace_collection (playwrightEnabled: true, same outputDir). Use the produced trace file and Playwright zip as inputs for generation for that specific recommendation.\n\nFor all other types in the top N, for each recommendation call the corresponding generation tool using an endpoint URL derived from that recommendation (api.baseUrl + endpoint path, or a full endpoint URL included in the recommendation). If a recommendation does not specify a concrete endpoint, follow the scope indicated in the recommendation (e.g. service-wide or repo-wide) and omit endpointURL or use only api.baseUrl as appropriate. In all cases also pass apiSchema, language, framework, and outputDir from workspace.yml. Capture each generated test file path for step 3.",
        conditionalGuidance: "Tool mapping by type — smoke: skyramp_smoke_test_generation, contract: skyramp_contract_test_generation, integration: skyramp_integration_test_generation, fuzz: skyramp_fuzz_test_generation, load: skyramp_load_test_generation, e2e: skyramp_e2e_test_generation, ui: skyramp_ui_test_generation. Limit to top N types. For UI/E2E: for each recommendation, (1) search the repo for existing traces for the endpoint or path associated with that recommendation; (2) if found, use them directly; (3) if not found, run skyramp_start_trace_collection → user interaction with the recommended endpoint/path → skyramp_stop_trace_collection, then use the produced traces. Record each generated test file path for step 3.",
    },
    {
        stepIndex: 3,
        title: "Execute generated tests",
        description: "Before calling skyramp_execute_test, resolve the auth token using only information available to the workflow: (1) if the user provided a token param, use it directly; (2) otherwise, check .skyramp/workspace.yml or other repo config for a documented token value; (3) if no token can be resolved, proceed with empty string '' and rely on skyramp_execute_test to surface 401/403 errors — then report the auth failure to the user and ask them to provide a Bearer token to re-run. Then for each test file generated in step 2, call skyramp_execute_test with the test file path, language, testType, and the resolved token.",
        toolCall: {
            toolName: "skyramp_execute_test",
            description: "Execute each generated test; resolve token before calling. For each generated test file path returned directly by the generation tools in step 2, invoke this tool with testFile set to that path.",
            inputs: {
                workspacePath: { source: "user", paramKey: "repositoryPath" },
                language: { source: "literal", value: "from workspace" },
                testType: { source: "literal", value: "from test file / recommendation" },
                testFile: {
                    source: "literal",
                    value: "path to a single generated test file; for each test path returned by generation tools in step 2, call skyramp_execute_test separately with testFile set to that path",
                },
                token: { source: "user", paramKey: "token" },
            },
        },
        conditionalGuidance: "Skip if step 2 generated no tests. Iterate over each generated test file path returned directly from the tools invoked in step 2 and call skyramp_execute_test once per file. Token resolution: (1) user-provided token; (2) token from .skyramp/workspace.yml or repo config; (3) empty string '' — let skyramp_execute_test surface auth errors, then ask the user for a Bearer token to re-run.",
    },
];
export const FULLREPO_RECOMMEND_GENERATE_EXECUTE_TOPN_TESTS_COMMAND = {
    id: "full_repo_scan_recommend_generate_and_execute_top_n_tests",
    name: "Full Repo: Recommend, Generate and Run TopN Tests",
    description: "Run skyramp_analyze_changes to scan the repo and get ranked recommendations, generate tests for the top N recommended types, then execute the generated tests.",
    intent: {
        contextIndicators: [
            "Use when the user wants to scan the entire repository with no specific endpoint or PR diff in mind — to get ranked test recommendations across all endpoints, generate the top N recommended test types, and execute them",
            "Scope is always the full repository — the user has not mentioned a specific endpoint URL, path, or name",
            "Use when the user asks to recommend, generate, and execute tests for the whole repo or says something like 'run the full repo test workflow'",
            "Do NOT use when the user specifies a particular endpoint — use test_given_endpoint_comprehensively instead",
            "Do NOT use when the user asks about a PR diff or branch-scoped analysis — use skyramp_analyze_changes directly instead",
            "Do NOT use for simple single-tool requests such as 'generate a smoke test' or 'recommend tests for this PR'",
        ],
        purpose: "Full repo scan: get recommendations → Generate top N types → Execute generated tests (no specific endpoint, no PR diff). Cleanup is handled automatically.",
        workflowSummary: "Full Repo Scan → Recommend → Generate top N → Execute each test (cleanup is automatic)",
        examples: {
            use: [
                "scan the full repo and recommend and execute top 3 tests",
                "run the full repo test workflow",
                "recommend generate and execute tests for the whole repo",
                "find the best tests to write for this codebase and run them",
            ],
            doNotUse: [
                "comprehensively test the products endpoint → has a specific endpoint, use test_given_endpoint_comprehensively instead",
                "recommend tests for my PR diff → branch-scoped, use skyramp_analyze_changes directly",
                "generate a smoke test → single tool request, call skyramp_smoke_test_generation directly",
            ],
        },
    },
    steps: fullRepoRecommendGenerateExecuteTopNSteps,
    inputParams: {
        required: ["repositoryPath"],
        optional: [
            "topN",
            "token",
            "endpointURL",
            "apiSchema",
        ],
    },
};
