export const OPENAPI_SPEC = {
  openapi: '3.0.3',
  info: {
    title: 'arcrun cypher-executor API',
    description: 'AI Workflow Execution Engine — 透過三元組 Triplet 或圖 Graph 定義工作流，系統執行並回傳結果',
    version: '1.0.0',
    contact: {
      name: 'arcrun',
      url: 'https://github.com/arcrun/arcrun',
    },
  },
  servers: [
    { url: 'https://cypher.arcrun.dev', description: 'arcrun.dev Hosted' },
    { url: 'http://localhost:8787', description: 'Local Development' },
  ],
  paths: {
    '/': {
      get: {
        summary: 'Health Check',
        tags: ['Health'],
        responses: {
          '200': {
            description: 'Service is running',
            content: {
              'application/json': {
                schema: {
                  type: 'object',
                  properties: {
                    service: { type: 'string' },
                    version: { type: 'string' },
                    status: { type: 'string' },
                  },
                },
              },
            },
          },
        },
      },
    },
    '/cypher/search': {
      post: {
        summary: '搜尋工作流需要的零件',
        tags: ['Cypher'],
        description: '用三元組描述工作流，系統解析並從 Registry 查詢對應零件',
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                properties: {
                  triplets: {
                    type: 'array',
                    items: { type: 'string' },
                    example: ['start >> 完成後 >> get-data', 'get-data >> 完成後 >> done'],
                    description: '三元組陣列，格式：\"FROM >> ACTION >> TO\"',
                  },
                  auto_publish: {
                    type: 'boolean',
                    default: true,
                    description: '缺失的零件是否自動產生發佈',
                  },
                },
                required: ['triplets'],
              },
            },
          },
        },
        responses: {
          '200': {
            description: '零件搜尋成功（含版本號和時戳，適合 Markdown 文檔追蹤）',
            content: {
              'application/json': {
                schema: {
                  type: 'object',
                  properties: {
                    version: { type: 'string', example: 'search-v1-20260327-143022', description: '版本號（endpoint-v{major}-{timestamp}）' },
                    timestamp: { type: 'string', format: 'date-time', description: 'ISO 8601 時戳' },
                    triplets: { type: 'array', items: { type: 'string' }, description: '回送的三元組列表' },
                    nodes: { type: 'object', description: '搜尋到的零件及其狀態' },
                    cypher: { type: 'object', description: '工作流圖（null 若有缺失零件）' },
                    missing: { type: 'array', items: { type: 'string' }, description: '缺失零件列表' },
                    auto_published: { type: 'object', description: '自動發佈的零件（若 auto_publish=true）' },
                  },
                },
              },
            },
          },
          '400': { description: '無法解析三元組' },
        },
      },
    },
    '/cypher/execute': {
      post: {
        summary: '執行工作流',
        tags: ['Cypher'],
        description: '直接執行 triplets，回傳完整執行結果。支援自動發佈缺失零件。',
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                properties: {
                  triplets: {
                    type: 'array',
                    items: { type: 'string' },
                    description: '三元組陣列，格式："FROM >> ACTION >> TO"',
                  },
                  context: {
                    type: 'object',
                    description: '執行上下文，傳入各節點作為初始參數',
                  },
                  auto_publish: {
                    type: 'boolean',
                    default: true,
                    description: '缺失的零件是否自動產生臨時實作',
                  },
                },
                required: ['triplets'],
              },
            },
          },
        },
        responses: {
          '200': {
            description: '執行成功（含版本號和時戳）',
            content: {
              'application/json': {
                schema: {
                  type: 'object',
                  properties: {
                    version: { type: 'string', example: 'execute-v1-20260327-143022', description: '版本號（endpoint-v{major}-{timestamp}）' },
                    timestamp: { type: 'string', format: 'date-time', description: 'ISO 8601 時戳' },
                    success: { type: 'boolean', enum: [true] },
                    data: { type: 'object', description: '執行結果' },
                    trace: { type: 'array', description: '執行跟蹤' },
                    duration_ms: { type: 'number' },
                  },
                },
              },
            },
          },
          '500': {
            description: '執行失敗或部份零件缺失（含版本號和時戳）',
            content: {
              'application/json': {
                schema: {
                  type: 'object',
                  properties: {
                    version: { type: 'string', example: 'execute-v1-20260327-143022', description: '版本號（endpoint-v{major}-{timestamp}）' },
                    timestamp: { type: 'string', format: 'date-time', description: 'ISO 8601 時戳' },
                    success: { type: 'boolean', enum: [false] },
                    error: { type: 'string' },
                    missing: { type: 'array', items: { type: 'string' }, description: '無法自動發佈的缺失零件' },
                    auto_published: {
                      type: 'object',
                      description: '自動發佈的零件資訊',
                      additionalProperties: {
                        type: 'object',
                        properties: {
                          ok: { type: 'boolean' },
                          componentId: { type: 'string' },
                          temporary_endpoint: { type: 'string', format: 'uri', description: '臨時實作的 URL' },
                          implement_by: { type: 'string', format: 'date-time', description: '實作截止時間' },
                        },
                      },
                    },
                    duration_ms: { type: 'number' },
                  },
                },
              },
            },
          },
        },
      },
    },
    '/webhooks': {
      post: {
        summary: '建立 Webhook',
        tags: ['Webhooks'],
        description: '將工作流註冊成 Webhook，得到公開 URL',
        requestBody: {
          required: true,
          content: {
            'application/json': {
              schema: {
                type: 'object',
                properties: {
                  triplets: {
                    type: 'array',
                    items: { type: 'string' },
                  },
                  description: { type: 'string' },
                },
              },
            },
          },
        },
        responses: {
          '201': {
            description: 'Webhook 建立成功',
            content: {
              'application/json': {
                schema: {
                  type: 'object',
                  properties: {
                    token: { type: 'string' },
                    webhook_url: { type: 'string', format: 'uri' },
                    description: { type: 'string' },
                    created_at: { type: 'string', format: 'date-time' },
                  },
                },
              },
            },
          },
        },
      },
      get: {
        summary: '列出所有 Webhooks',
        tags: ['Webhooks'],
        parameters: [
          {
            name: 'Authorization',
            in: 'header',
            required: true,
            schema: { type: 'string', example: 'Bearer u6u_xxxxx' },
            description: 'API Key 認證',
          },
        ],
        responses: {
          '200': {
            description: 'Webhooks 列表',
            content: {
              'application/json': {
                schema: {
                  type: 'object',
                  properties: {
                    webhooks: {
                      type: 'array',
                      items: {
                        type: 'object',
                        properties: {
                          token: { type: 'string' },
                          description: { type: 'string' },
                          created_at: { type: 'string', format: 'date-time' },
                        },
                      },
                    },
                    total: { type: 'number' },
                  },
                },
              },
            },
          },
          '401': { description: '未授權' },
        },
      },
    },
    '/webhooks/{token}': {
      get: {
        summary: '查詢單個 Webhook',
        tags: ['Webhooks'],
        parameters: [
          {
            name: 'token',
            in: 'path',
            required: true,
            schema: { type: 'string' },
          },
        ],
        responses: {
          '200': {
            description: 'Webhook 資訊',
          },
          '404': { description: 'Webhook 不存在' },
        },
      },
      delete: {
        summary: '刪除 Webhook',
        tags: ['Webhooks'],
        parameters: [
          {
            name: 'token',
            in: 'path',
            required: true,
            schema: { type: 'string' },
          },
        ],
        responses: {
          '200': { description: 'Webhook 已刪除' },
          '404': { description: 'Webhook 不存在' },
        },
      },
    },
  },
  components: {
    securitySchemes: {
      ApiKeyAuth: {
        type: 'apiKey',
        in: 'header',
        name: 'Authorization',
      },
    },
  },
};
