import { CursorIcon } from '@/components/icons'
import type { BlockConfig } from '@/blocks/types'
import { AuthMode, IntegrationType } from '@/blocks/types'
import { createVersionedToolSelector } from '@/blocks/utils'
import type { CursorResponse } from '@/tools/cursor/types'

export const CursorBlock: BlockConfig<CursorResponse> = {
  type: 'cursor',
  name: 'Cursor (Legacy)',
  description: 'Launch and manage Cursor cloud agents to work on GitHub repositories',
  longDescription:
    'Interact with Cursor Cloud Agents API to launch AI agents that can work on your GitHub repositories. Supports launching agents, adding follow-up instructions, checking status, viewing conversations, and managing agent lifecycle.',
  docsLink: 'https://cursor.com/docs/cloud-agent/api/endpoints',
  category: 'tools',
  integrationType: IntegrationType.DeveloperTools,
  tags: ['agentic', 'automation'],
  bgColor: '#1E1E1E',
  icon: CursorIcon,
  authMode: AuthMode.ApiKey,
  hideFromToolbar: true,
  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'Launch Agent', id: 'cursor_launch_agent' },
        { label: 'Add Follow-up', id: 'cursor_add_followup' },
        { label: 'Get Agent Status', id: 'cursor_get_agent' },
        { label: 'Get Conversation', id: 'cursor_get_conversation' },
        { label: 'List Agents', id: 'cursor_list_agents' },
        { label: 'Stop Agent', id: 'cursor_stop_agent' },
        { label: 'Delete Agent', id: 'cursor_delete_agent' },
        { label: 'List Artifacts', id: 'cursor_list_artifacts' },
        { label: 'Download Artifact', id: 'cursor_download_artifact' },
      ],
      value: () => 'cursor_launch_agent',
    },
    {
      id: 'repository',
      title: 'Repository URL',
      type: 'short-input',
      placeholder: 'https://github.com/your-org/your-repo',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
      required: true,
    },
    {
      id: 'ref',
      title: 'Branch/Ref',
      type: 'short-input',
      placeholder: 'main (optional)',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
      mode: 'advanced',
    },
    {
      id: 'promptText',
      title: 'Prompt',
      type: 'long-input',
      placeholder: 'Describe the task for the agent...',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
      required: true,
    },
    {
      id: 'promptImages',
      title: 'Prompt Images',
      type: 'long-input',
      placeholder: '[{"data": "base64...", "dimension": {"width": 1024, "height": 768}}]',
      condition: { field: 'operation', value: ['cursor_launch_agent', 'cursor_add_followup'] },
      mode: 'advanced',
    },
    {
      id: 'model',
      title: 'Model',
      type: 'short-input',
      placeholder: 'Auto-selection by default',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
      mode: 'advanced',
    },
    {
      id: 'branchName',
      title: 'Branch Name',
      type: 'short-input',
      placeholder: 'Custom branch name (optional)',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
      mode: 'advanced',
    },
    {
      id: 'autoCreatePr',
      title: 'Auto Create PR',
      type: 'switch',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
    },
    {
      id: 'openAsCursorGithubApp',
      title: 'Open as Cursor GitHub App',
      type: 'switch',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
      mode: 'advanced',
    },
    {
      id: 'skipReviewerRequest',
      title: 'Skip Reviewer Request',
      type: 'switch',
      condition: { field: 'operation', value: 'cursor_launch_agent' },
      mode: 'advanced',
    },
    {
      id: 'agentId',
      title: 'Agent ID',
      type: 'short-input',
      placeholder: 'e.g., bc_abc123',
      condition: {
        field: 'operation',
        value: [
          'cursor_get_agent',
          'cursor_get_conversation',
          'cursor_add_followup',
          'cursor_stop_agent',
          'cursor_delete_agent',
          'cursor_list_artifacts',
          'cursor_download_artifact',
        ],
      },
      required: true,
    },
    {
      id: 'path',
      title: 'Artifact Path',
      type: 'short-input',
      placeholder: '/opt/cursor/artifacts/screenshot.png',
      condition: { field: 'operation', value: 'cursor_download_artifact' },
      required: true,
    },
    {
      id: 'followupPromptText',
      title: 'Follow-up Prompt',
      type: 'long-input',
      placeholder: 'Additional instructions for the agent...',
      condition: { field: 'operation', value: 'cursor_add_followup' },
      required: true,
    },
    {
      id: 'prUrl',
      title: 'PR URL Filter',
      type: 'short-input',
      placeholder: 'Filter by pull request URL (optional)',
      condition: { field: 'operation', value: 'cursor_list_agents' },
      mode: 'advanced',
    },
    {
      id: 'limit',
      title: 'Limit',
      type: 'short-input',
      placeholder: '20 (default, max 100)',
      condition: { field: 'operation', value: 'cursor_list_agents' },
      mode: 'advanced',
    },
    {
      id: 'cursor',
      title: 'Pagination Cursor',
      type: 'short-input',
      placeholder: 'Cursor from previous response',
      condition: { field: 'operation', value: 'cursor_list_agents' },
      mode: 'advanced',
    },
    {
      id: 'apiKey',
      title: 'API Key',
      type: 'short-input',
      placeholder: 'Enter Cursor API Key',
      password: true,
      required: true,
    },
  ],
  tools: {
    access: [
      'cursor_list_agents',
      'cursor_get_agent',
      'cursor_get_conversation',
      'cursor_launch_agent',
      'cursor_add_followup',
      'cursor_stop_agent',
      'cursor_delete_agent',
      'cursor_list_artifacts',
      'cursor_download_artifact',
    ],
    config: {
      tool: (params) => params.operation || 'cursor_launch_agent',
    },
  },
  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    repository: { type: 'string', description: 'GitHub repository URL' },
    ref: { type: 'string', description: 'Branch, tag, or commit reference' },
    promptText: { type: 'string', description: 'Instruction text for the agent' },
    followupPromptText: { type: 'string', description: 'Follow-up instruction text for the agent' },
    promptImages: {
      type: 'string',
      description: 'JSON array of image objects with base64 data and dimensions',
    },
    model: { type: 'string', description: 'Model to use (empty for auto-selection)' },
    branchName: { type: 'string', description: 'Custom branch name' },
    autoCreatePr: { type: 'boolean', description: 'Auto-create PR when done' },
    openAsCursorGithubApp: { type: 'boolean', description: 'Open PR as Cursor GitHub App' },
    skipReviewerRequest: { type: 'boolean', description: 'Skip reviewer request' },
    agentId: { type: 'string', description: 'Agent identifier' },
    prUrl: { type: 'string', description: 'Filter agents by pull request URL' },
    limit: { type: 'number', description: 'Number of results to return' },
    cursor: { type: 'string', description: 'Pagination cursor' },
    path: { type: 'string', description: 'Absolute path of the artifact to download' },
    apiKey: { type: 'string', description: 'Cursor API key' },
  },
  outputs: {
    content: { type: 'string', description: 'Human-readable response content' },
    metadata: { type: 'json', description: 'Response metadata' },
  },
}

export const CursorV2Block: BlockConfig<CursorResponse> = {
  ...CursorBlock,
  type: 'cursor_v2',
  name: 'Cursor',
  description: 'Launch and manage Cursor cloud agents to work on GitHub repositories',
  longDescription:
    'Interact with Cursor Cloud Agents API to launch AI agents that can work on your GitHub repositories. Supports launching agents, adding follow-up instructions, checking status, viewing conversations, and managing agent lifecycle.',
  hideFromToolbar: false,
  tools: {
    ...CursorBlock.tools,
    access: [
      'cursor_list_agents_v2',
      'cursor_get_agent_v2',
      'cursor_get_conversation_v2',
      'cursor_launch_agent_v2',
      'cursor_add_followup_v2',
      'cursor_stop_agent_v2',
      'cursor_delete_agent_v2',
      'cursor_list_artifacts_v2',
      'cursor_download_artifact_v2',
    ],
    config: {
      tool: createVersionedToolSelector({
        baseToolSelector: (params) => params.operation || 'cursor_launch_agent',
        suffix: '_v2',
        fallbackToolId: 'cursor_launch_agent_v2',
      }),
    },
  },
  outputs: {
    id: { type: 'string', description: 'Agent identifier' },
    url: { type: 'string', description: 'Agent URL (launch operation)' },
    name: { type: 'string', description: 'Agent name' },
    status: { type: 'string', description: 'Agent status' },
    source: { type: 'json', description: 'Agent source repository info' },
    target: { type: 'json', description: 'Agent target branch/PR info' },
    summary: { type: 'string', description: 'Agent summary' },
    createdAt: { type: 'string', description: 'Agent creation timestamp' },
    agents: { type: 'json', description: 'Array of agent objects (list operation)' },
    nextCursor: { type: 'string', description: 'Pagination cursor (list operation)' },
    messages: { type: 'json', description: 'Conversation messages (get conversation operation)' },
    artifacts: { type: 'json', description: 'List of artifact files (list artifacts operation)' },
    file: { type: 'file', description: 'Downloaded artifact file (download artifact operation)' },
  },
}
