import type { ToolsetDefinition } from "../types.js";
import { ngExtract, pageExtract } from "../extractors.js";

export const infrastructureToolset: ToolsetDefinition = {
  name: "infrastructure",
  displayName: "Infrastructure",
  description: "Infrastructure definitions for deployment targets",
  resources: [
    {
      resourceType: "infrastructure",
      displayName: "Infrastructure Definition",
      description: "Infrastructure definition within an environment. Supports full CRUD. Use resource_scope='account' to list or get account-level infrastructure definitions.",
      toolset: "infrastructure",
      scope: "project",
      supportedScopes: ["account", "org", "project"],
      identifierFields: ["infrastructure_id"],
      listFilterFields: [
        { name: "environment_id", description: "**Required.** Environment identifier — infrastructure is always scoped to an environment" },
        { name: "search_term", description: "Search term to filter infrastructure definitions" },
        { name: "deployment_type", description: "Filter by deployment type (e.g. Kubernetes, ECS)" },
        { name: "sort", description: "Field to sort by (e.g. name, identifier)" },
        { name: "order", description: "Sort order", enum: ["asc", "desc"] },
      ],
      deepLinkTemplate: "/ng/account/{accountId}/all/orgs/{orgIdentifier}/projects/{projectIdentifier}/environments",
      operations: {
        list: {
          method: "GET",
          path: "/ng/api/infrastructures",
          operationPolicy: { risk: "read", retryPolicy: "safe" },
          queryParams: {
            environment_id: "environmentIdentifier",
            search_term: "searchTerm",
            deployment_type: "deploymentType",
            sort: "sort",
            order: "order",
            page: "page",
            size: "size",
          },
          responseExtractor: pageExtract,
          description: "List infrastructure definitions",
        },
        get: {
          method: "GET",
          path: "/ng/api/infrastructures/{infraIdentifier}",
          operationPolicy: { risk: "read", retryPolicy: "safe" },
          pathParams: { infrastructure_id: "infraIdentifier" },
          queryParams: { environment_id: "environmentIdentifier" },
          responseExtractor: ngExtract,
          description: "Get infrastructure definition details",
        },
        create: {
          method: "POST",
          path: "/ng/api/infrastructures",
          operationPolicy: { risk: "low_write", retryPolicy: "do_not_retry" },
          bodyBuilder: (input) => input.body,
          bodySchema: {
            description: "Infrastructure definition",
            fields: [
              { name: "identifier", type: "string", required: true, description: "Unique identifier" },
              { name: "name", type: "string", required: true, description: "Display name" },
              { name: "type", type: "string", required: true, description: "Infrastructure type (e.g. KubernetesDirect, KubernetesGcp)" },
              { name: "environmentRef", type: "string", required: true, description: "Environment reference identifier" },
              { name: "deploymentType", type: "string", required: false, description: "Deployment type (e.g. Kubernetes)" },
              { name: "yaml", type: "yaml", required: false, description: "Full infrastructure YAML definition" },
            ],
          },
          responseExtractor: ngExtract,
          description: "Create infrastructure definition",
        },
        update: {
          method: "PUT",
          path: "/ng/api/infrastructures",
          operationPolicy: { risk: "low_write", retryPolicy: "safe" },
          bodyBuilder: (input) => input.body,
          bodySchema: {
            description: "Infrastructure definition update",
            fields: [
              { name: "identifier", type: "string", required: true, description: "Infrastructure identifier" },
              { name: "name", type: "string", required: true, description: "Display name" },
              { name: "type", type: "string", required: true, description: "Infrastructure type" },
              { name: "environmentRef", type: "string", required: true, description: "Environment reference identifier" },
              { name: "yaml", type: "yaml", required: false, description: "Full infrastructure YAML definition" },
            ],
          },
          responseExtractor: ngExtract,
          description: "Update infrastructure definition",
        },
        delete: {
          method: "DELETE",
          path: "/ng/api/infrastructures/{infraIdentifier}",
          operationPolicy: { risk: "destructive", retryPolicy: "do_not_retry" },
          pathParams: { infrastructure_id: "infraIdentifier" },
          queryParams: { environment_id: "environmentIdentifier" },
          responseExtractor: ngExtract,
          description: "Delete infrastructure definition",
        },
      },
      executeActions: {
        move_configs: {
          method: "POST",
          path: "/ng/api/infrastructures/move-config/{infraIdentifier}",
          operationPolicy: { risk: "low_write", retryPolicy: "do_not_retry" },
          pathParams: { infrastructure_id: "infraIdentifier" },
          queryParams: {
            environment_id: "environmentIdentifier",
            connector_ref: "connectorRef",
            repo_name: "repoName",
            branch: "branch",
            file_path: "filePath",
            commit_msg: "commitMsg",
            is_new_branch: "isNewBranch",
            base_branch: "baseBranch",
            is_harness_code_repo: "isHarnessCodeRepo",
            move_config_type: "moveConfigType",
          },
          bodyBuilder: () => ({}),
          bodySchema: {
            description: "Move configuration request. All parameters are passed as query params.",
            fields: [
              { name: "environment_id", type: "string", required: true, description: "Environment identifier" },
              { name: "connector_ref", type: "string", required: false, description: "Connector reference for remote storage" },
              { name: "repo_name", type: "string", required: false, description: "Repository name" },
              { name: "branch", type: "string", required: false, description: "Branch name" },
              { name: "file_path", type: "string", required: false, description: "File path in the repository" },
              { name: "commit_msg", type: "string", required: false, description: "Commit message" },
              { name: "is_new_branch", type: "boolean", required: false, description: "Whether to create a new branch" },
              { name: "base_branch", type: "string", required: false, description: "Base branch if creating a new branch" },
              { name: "is_harness_code_repo", type: "boolean", required: false, description: "Whether the repo is a Harness Code repo" },
              { name: "move_config_type", type: "string", required: true, description: "INLINE_TO_REMOTE or REMOTE_TO_INLINE" },
            ],
          },
          responseExtractor: ngExtract,
          actionDescription: "Move infrastructure configuration (e.g., move inline config to remote or vice versa)",
        },
      },
    },
  ],
};
