export type PublicApiMethod = 'DELETE' | 'GET' | 'PATCH' | 'POST';
export type PublicApiSurface = 'compatibility' | 'open';
export type PublicApiFamily =
  | 'account'
  | 'discovery'
  | 'gemini'
  | 'generation'
  | 'kling'
  | 'models'
  | 'portrait'
  | 'security'
  | 'serverMain'
  | 'tasks'
  | 'volces';

export interface PublicApiEndpoint {
  description: string;
  method: PublicApiMethod;
  path: string;
}

export interface PublicApiCatalogGroup {
  description: string;
  endpoints: PublicApiEndpoint[];
  family: PublicApiFamily;
  surface: PublicApiSurface;
  title: string;
}

export const publicApiCatalogGroups: PublicApiCatalogGroup[] = [
  {
    family: 'discovery',
    surface: 'open',
    title: '运行状态与接口发现',
    description: '健康检查、OpenAPI 定义以及无需登录即可读取的公开目录和运维资源。',
    endpoints: [
      { method: 'GET', path: '/api/v1/healthz', description: '存活检查' },
      { method: 'GET', path: '/api/v1/readyz', description: '就绪检查' },
      { method: 'GET', path: '/api/v1/openapi.json', description: 'OpenAPI JSON' },
      { method: 'GET', path: '/api/v1/openapi.yaml', description: 'OpenAPI YAML' },
      { method: 'GET', path: '/api/v1/public/identity', description: '公开身份配置' },
      { method: 'GET', path: '/api/v1/public/client-customization', description: '公开客户端配置' },
      { method: 'GET', path: '/api/v1/public/catalog/providers', description: '公开供应商目录' },
      { method: 'GET', path: '/api/v1/public/catalog/base-models', description: '公开基础模型目录' },
      { method: 'GET', path: '/api/v1/public/skills/ai-gateway-ops-management/metadata', description: '运维 Skill 元数据' },
      { method: 'GET', path: '/api/v1/public/skills/ai-gateway-ops-management/download', description: '下载运维 Skill' },
    ],
  },
  {
    family: 'account',
    surface: 'open',
    title: '账号、授权与 API Key',
    description: '账号登录使用 JWT 或 OIDC，会话建立后可创建和管理供开放接口使用的 Gateway API Key。',
    endpoints: [
      { method: 'POST', path: '/api/v1/auth/register', description: '注册本地账号' },
      { method: 'POST', path: '/api/v1/auth/login', description: '本地账号登录' },
      { method: 'GET', path: '/api/v1/auth/oidc/login', description: '发起 OIDC 登录' },
      { method: 'GET', path: '/api/v1/auth/oidc/callback', description: 'OIDC 回调' },
      { method: 'POST', path: '/api/v1/auth/oidc/logout', description: 'OIDC 登出' },
      { method: 'DELETE', path: '/api/v1/auth/oidc/session', description: '删除浏览器会话' },
      { method: 'GET', path: '/api/v1/me', description: '查询当前用户' },
      { method: 'GET', path: '/api/v1/api-keys', description: '查询 API Key' },
      { method: 'POST', path: '/api/v1/api-keys', description: '创建 API Key' },
      { method: 'GET', path: '/api/v1/api-keys/access-rules', description: '查询 Key 访问规则' },
      { method: 'POST', path: '/api/v1/api-keys/access-rules/batch', description: '批量设置 Key 访问规则' },
      { method: 'GET', path: '/api/v1/api-keys/assignable-models', description: '查询可分配模型' },
      { method: 'PATCH', path: '/api/v1/api-keys/{apiKeyID}/scopes', description: '更新 Key 权限范围' },
      { method: 'PATCH', path: '/api/v1/api-keys/{apiKeyID}/disable', description: '禁用 Key' },
      { method: 'DELETE', path: '/api/v1/api-keys/{apiKeyID}', description: '删除 Key' },
    ],
  },
  {
    family: 'models',
    surface: 'open',
    title: '模型、平台与计费',
    description: '查询账号可用的模型、平台、模型能力和 Playground 目录，并在提交前预估费用。',
    endpoints: [
      { method: 'GET', path: '/api/v1/model-catalog', description: '模型能力目录' },
      { method: 'GET', path: '/api/v1/platforms', description: '当前用户可用平台' },
      { method: 'GET', path: '/api/v1/models', description: '当前用户可用模型' },
      { method: 'GET', path: '/api/v1/playground/models', description: 'Playground 可用模型' },
      { method: 'POST', path: '/api/v1/pricing/estimate', description: '请求价格预估' },
    ],
  },
  {
    family: 'generation',
    surface: 'open',
    title: '通用生成接口',
    description: '统一使用 Gateway 模型名和参数，覆盖文本、图片、视频、音乐、语音和文件上传。',
    endpoints: [
      { method: 'POST', path: '/api/v1/chat/completions', description: 'Chat Completions，支持 SSE' },
      { method: 'POST', path: '/api/v1/responses', description: 'Responses，支持 SSE' },
      { method: 'POST', path: '/api/v1/embeddings', description: '文本向量' },
      { method: 'POST', path: '/api/v1/reranks', description: '文本重排序' },
      { method: 'POST', path: '/api/v1/images/generations', description: '文生图' },
      { method: 'POST', path: '/api/v1/images/edits', description: '图片编辑' },
      { method: 'POST', path: '/api/v1/videos/generations', description: '文生视频、图生视频及多模态视频' },
      { method: 'POST', path: '/api/v1/song/generations', description: '歌曲生成' },
      { method: 'POST', path: '/api/v1/music/generations', description: '音乐生成' },
      { method: 'POST', path: '/api/v1/speech/generations', description: '语音生成' },
      { method: 'POST', path: '/api/v1/voice_clone', description: '声音克隆' },
      { method: 'GET', path: '/api/v1/voice_clone/voices', description: '查询克隆声音' },
      { method: 'DELETE', path: '/api/v1/voice_clone/voices/{voiceID}', description: '删除克隆声音' },
      { method: 'POST', path: '/api/v1/files/upload', description: '上传生成任务输入文件' },
    ],
  },
  {
    family: 'tasks',
    surface: 'open',
    title: '异步任务',
    description: '通用生成接口使用 X-Async: true 后，通过这些接口查询、取消任务并读取进度和预处理记录。',
    endpoints: [
      { method: 'GET', path: '/api/v1/tasks', description: '查询任务列表' },
      { method: 'GET', path: '/api/v1/tasks/{taskID}', description: '查询任务详情和结果' },
      { method: 'POST', path: '/api/v1/tasks/{taskID}/cancel', description: '取消任务' },
      { method: 'GET', path: '/api/v1/tasks/{taskID}/events', description: '查询任务事件' },
      { method: 'GET', path: '/api/v1/tasks/{taskID}/param-preprocessing', description: '查询参数预处理记录' },
    ],
  },
  {
    family: 'portrait',
    surface: 'open',
    title: '真人资产',
    description: '查询、上传并同步用户真人素材，供支持真人资产的通用视频模型使用。',
    endpoints: [
      { method: 'GET', path: '/api/v1/resource/material/seedance-portrait-assets/capability', description: '查询真人资产能力' },
      { method: 'GET', path: '/api/v1/resource/material/user/materials', description: '查询用户真人资产' },
      { method: 'POST', path: '/api/v1/resource/material', description: '上传真人资产' },
      { method: 'POST', path: '/api/v1/resource/material/seedance-portrait-assets/sync', description: '同步真人资产到平台' },
    ],
  },
  {
    family: 'security',
    surface: 'open',
    title: '安全集成',
    description: '按 RFC 8935 接收安全事件；该端点按 SSF 协议鉴权，不使用普通生成 API Key。',
    endpoints: [
      { method: 'POST', path: '/api/v1/security-events/ssf', description: 'RFC 8935 Security Event 接收端点' },
    ],
  },
  {
    family: 'gemini',
    surface: 'compatibility',
    title: 'Gemini 兼容接口',
    description: '保留 Gemini generateContent 与 Files 上传请求结构，调用方改用 Gateway Base URL 和 API Key。',
    endpoints: [
      { method: 'POST', path: '/api/v1/models/{model}:generateContent', description: 'Gemini generateContent' },
      { method: 'POST', path: '/api/v1/gemini/upload/{version}/files', description: 'Gemini Files 启动或直接上传' },
      { method: 'POST', path: '/api/v1/gemini/upload/{version}/files/{uploadID}', description: '完成 Gemini 分段上传' },
    ],
  },
  {
    family: 'kling',
    surface: 'compatibility',
    title: '可灵兼容接口',
    description: '兼容中国区可灵 V1 AK/SK Omni 和 API 2.0 协议；调用方只使用 Gateway API Key。',
    endpoints: [
      { method: 'POST', path: '/api/v1/videos/omni-video', description: '可灵官方 V1 Omni 创建任务' },
      { method: 'GET', path: '/api/v1/videos/omni-video/{taskID}', description: '可灵官方 V1 Omni 查询任务' },
      { method: 'POST', path: '/api/v1/kling/v1/videos/omni-video', description: '网关可灵 V1 创建任务' },
      { method: 'GET', path: '/api/v1/kling/v1/videos/omni-video', description: '网关可灵 V1 任务列表' },
      { method: 'GET', path: '/api/v1/kling/v1/videos/omni-video/{taskID}', description: '网关可灵 V1 查询任务' },
      { method: 'POST', path: '/api/v1/kling/v2/omni-video/{model}', description: '网关可灵 API 2.0 创建任务' },
      { method: 'GET', path: '/api/v1/kling/v2/tasks', description: '网关可灵 API 2.0 查询任务' },
      { method: 'POST', path: '/api/v1/kling/v2/tasks', description: '网关可灵 API 2.0 任务列表' },
    ],
  },
  {
    family: 'volces',
    surface: 'compatibility',
    title: '火山兼容接口',
    description: '兼容火山内容生成任务协议及任务列表、查询、取消操作。',
    endpoints: [
      { method: 'POST', path: '/api/v1/contents/generations/tasks', description: '创建火山内容生成任务' },
      { method: 'GET', path: '/api/v1/contents/generations/tasks', description: '查询火山内容生成任务列表' },
      { method: 'GET', path: '/api/v1/contents/generations/tasks/{taskID}', description: '查询火山内容生成任务' },
      { method: 'DELETE', path: '/api/v1/contents/generations/tasks/{taskID}', description: '删除或取消火山内容生成任务' },
    ],
  },
  {
    family: 'serverMain',
    surface: 'compatibility',
    title: 'server-main 兼容接口',
    description: '兼容 server-main 的视频提交与结果轮询响应结构，便于现有调用方只替换 Base URL 和 Key。',
    endpoints: [
      { method: 'POST', path: '/api/v1/video/generations', description: 'server-main 兼容视频创建接口' },
      { method: 'GET', path: '/api/v1/ai/result/{taskID}', description: 'server-main 兼容结果查询接口' },
    ],
  },
];

export function publicApiEndpointCount(surface?: PublicApiSurface) {
  return publicApiCatalogGroups
    .filter((group) => !surface || group.surface === surface)
    .reduce((total, group) => total + group.endpoints.length, 0);
}
