// u6u-builtins Worker 型別定義

export type Bindings = {
  REGISTRY: Fetcher;         // Component Registry Service Binding
  CYPHER: Fetcher;           // Cypher Executor Service Binding（排程執行用）
  U6U_STORE: KVNamespace;    // KV Store（cron: + ai-transform: 前綴）
  AI: Ai;                    // Workers AI（ai-transform compile 用）
  WORKER_BASE_URL: string;   // 本 Worker 對外 URL（用於上架時填入 url 欄位）
  ENVIRONMENT: string;
};

export type ActionResponse<T = unknown> =
  | { success: true; data: T }
  | { success: false; error: string };

// componentDefs：所有內建零件的定義清單（資料層，initComponents.ts 使用）
export interface ComponentDef {
  id: string;
  name: string;
  description: string;
  url: string;
  method: 'POST';
  tags: string;
  input_schema: object;
  output_schema: object;
}

// CronJob：排程定義（儲存在 U6U_STORE，key = cron:{id}）
export interface CronJob {
  id: string;
  cron_expr: string;         // 標準 5 欄位 cron expression
  triplets?: string[];       // 三元組格式工作流（與 graph_token 二選一）
  graph_token?: string;      // 已存在的 webhook token
  description: string;
  created_at: string;
  last_run?: string;
  enabled: boolean;
}

// AiTransform：已編譯的 AI 轉換函式（儲存在 U6U_STORE，key = ai-transform:{id}）
export interface AiTransform {
  id: string;
  description: string;       // 自然語言描述
  fn_body: string;           // 產生的 JS 函式 body（可直接 new Function 執行）
  created_at: string;
}

export function buildComponentDefs(baseUrl: string): ComponentDef[] {
  return [
    // === 既有零件 ===
    { id: 'http-request', name: 'http-request',
      description: '發送 HTTP 請求（GET/POST/PUT/DELETE），回傳 status 和 response body。支援自訂 headers 和 body。',
      url: `${baseUrl}/http-request`, method: 'POST', tags: 'builtin,http,request,api',
      input_schema: { type: 'object', required: ['url'], properties: { url: { type: 'string' }, method: { type: 'string', enum: ['GET','POST','PUT','DELETE','PATCH'] }, headers: { type: 'object' }, body: {} } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { status: { type: 'number' }, body: {} } } } } },
    { id: 'set', name: 'set',
      description: '設定變數（key-value），把結果傳遞到下一個節點。支援 assignments 陣列或 values 物件兩種格式。',
      url: `${baseUrl}/set`, method: 'POST', tags: 'builtin,variable,set,transform',
      input_schema: { type: 'object', properties: { assignments: { type: 'array', items: { type: 'object' } }, values: { type: 'object' }, context: { type: 'object' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object' } } } },
    { id: 'filter', name: 'filter',
      description: '依條件過濾陣列，回傳符合條件的元素。',
      url: `${baseUrl}/filter`, method: 'POST', tags: 'builtin,filter,array,condition',
      input_schema: { type: 'object', required: ['items','condition'], properties: { items: { type: 'array' }, condition: { type: 'object' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { items: { type: 'array' }, count: { type: 'number' } } } } } },
    { id: 'switch', name: 'switch',
      description: '依條件路由，多個出口分支。',
      url: `${baseUrl}/switch`, method: 'POST', tags: 'builtin,switch,branch,route,condition',
      input_schema: { type: 'object', required: ['value','cases'], properties: { value: {}, cases: { type: 'array' }, default_branch: { type: 'string' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { branch: { type: 'string' } } } } } },
    { id: 'merge', name: 'merge',
      description: '合併多個輸入物件為一個。',
      url: `${baseUrl}/merge`, method: 'POST', tags: 'builtin,merge,combine,object',
      input_schema: { type: 'object', required: ['inputs'], properties: { inputs: { type: 'array', items: { type: 'object' } } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object' } } } },
    { id: 'wait', name: 'wait',
      description: '等待指定毫秒數後繼續，最多 30 秒。',
      url: `${baseUrl}/wait`, method: 'POST', tags: 'builtin,wait,delay,throttle',
      input_schema: { type: 'object', required: ['ms'], properties: { ms: { type: 'number' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object' } } } },
    { id: 'google-sheets', name: 'google-sheets',
      description: '讀取或寫入 Google 試算表。需要 Google OAuth access_token。',
      url: `${baseUrl}/google-sheets`, method: 'POST', tags: 'integration,google,sheets,oauth',
      input_schema: { type: 'object', required: ['spreadsheet_id','range','access_token'], properties: { spreadsheet_id: { type: 'string' }, range: { type: 'string' }, action: { type: 'string', enum: ['read','write'] }, values: { type: 'array' }, access_token: { type: 'string' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object' } } } },
    { id: 'gmail', name: 'gmail',
      description: '透過 Gmail 發送 Email。需要 Google OAuth access_token。',
      url: `${baseUrl}/gmail`, method: 'POST', tags: 'integration,google,gmail,email,oauth',
      input_schema: { type: 'object', required: ['to','subject','body','access_token'], properties: { to: { type: 'string' }, subject: { type: 'string' }, body: { type: 'string' }, access_token: { type: 'string' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object' } } } },
    { id: 'line-notify', name: 'line-notify',
      description: '發送 LINE Notify 訊息。需要 LINE Channel Access Token。',
      url: `${baseUrl}/line-notify`, method: 'POST', tags: 'integration,line,notify,message',
      input_schema: { type: 'object', required: ['message','token'], properties: { message: { type: 'string' }, token: { type: 'string' }, image_url: { type: 'string' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object' } } } },
    { id: 'telegram', name: 'telegram',
      description: '透過 Telegram Bot 發送訊息。需要 bot_token 和 chat_id。',
      url: `${baseUrl}/telegram`, method: 'POST', tags: 'integration,telegram,bot,message',
      input_schema: { type: 'object', required: ['chat_id','text','bot_token'], properties: { chat_id: { type: 'string' }, text: { type: 'string' }, bot_token: { type: 'string' }, parse_mode: { type: 'string' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: {} } } },
    // === P1 新增：Cron ===
    { id: 'cron', name: 'cron',
      description: '建立定時排程工作流。指定 cron expression（如 0 9 * * *），到時間自動執行指定工作流。',
      url: `${baseUrl}/cron`, method: 'POST', tags: 'builtin,cron,schedule,trigger,timer',
      input_schema: { type: 'object', required: ['cron_expr'], properties: { cron_expr: { type: 'string', description: '標準 cron expression，如 0 9 * * *' }, triplets: { type: 'array', items: { type: 'string' } }, graph_token: { type: 'string' }, description: { type: 'string' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { cron_id: { type: 'string' }, cron_expr: { type: 'string' }, enabled: { type: 'boolean' } } } } } },
    // === P2 新增：控制流 ===
    { id: 'if', name: 'if',
      description: '單一條件判斷，true/false 兩個出口。condition 支援 JS 表達式（如 x > 5）。',
      url: `${baseUrl}/if`, method: 'POST', tags: 'builtin,control,if,branch,condition',
      input_schema: { type: 'object', required: ['condition'], properties: { condition: { type: 'string', description: 'JS 表達式，如 x > 5' }, input: { type: 'object', description: '提供給 condition 的變數' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { result: { type: 'boolean' }, branch: { type: 'string', enum: ['true', 'false'] } } } } } },
    { id: 'foreach', name: 'foreach',
      description: '對輸入陣列的每個元素執行一次後續工作流（依序）。',
      url: `${baseUrl}/foreach`, method: 'POST', tags: 'builtin,control,foreach,loop,iteration',
      input_schema: { type: 'object', required: ['items'], properties: { items: { type: 'array', description: '要迭代的陣列' }, item_key: { type: 'string', description: '每個元素注入的變數名，預設 item' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { items: { type: 'array' }, count: { type: 'number' }, current_index: { type: 'number' }, current_item: {} } } } } },
    { id: 'try-catch', name: 'try-catch',
      description: '錯誤處理分支：執行失敗時走 catch 出口繼續，不中斷整個工作流。',
      url: `${baseUrl}/try-catch`, method: 'POST', tags: 'builtin,control,try,catch,error,handling',
      input_schema: { type: 'object', required: ['action'], properties: { action: { type: 'object', description: '要嘗試執行的動作（url + body）' }, fallback: { description: '失敗時的預設輸出' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { branch: { type: 'string', enum: ['try', 'catch'] }, result: {}, error: { type: 'string' } } } } } },
    // === P3 新增：資料處理 ===
    { id: 'string-ops', name: 'string-ops',
      description: '字串操作：capitalize, trim, replace, split, join, substring, upper, lower, includes, startsWith, endsWith, regex match/extract/replace。',
      url: `${baseUrl}/string-ops`, method: 'POST', tags: 'builtin,data,string,transform,text',
      input_schema: { type: 'object', required: ['operation','input'], properties: { operation: { type: 'string' }, input: { type: 'string' }, args: { description: '操作參數（依 operation 而定）' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { result: {}, operation: { type: 'string' } } } } } },
    { id: 'number-ops', name: 'number-ops',
      description: '數字操作：round, floor, ceil, abs, format, add, subtract, multiply, divide, mod, min, max, clamp。',
      url: `${baseUrl}/number-ops`, method: 'POST', tags: 'builtin,data,number,math,transform',
      input_schema: { type: 'object', required: ['operation','input'], properties: { operation: { type: 'string' }, input: { type: 'number' }, args: { description: '操作參數（依 operation 而定）' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { result: {}, operation: { type: 'string' } } } } } },
    { id: 'array-ops', name: 'array-ops',
      description: '陣列操作：map, sort, max, min, sum, average, count, first, last, flatten, unique, reverse, chunk。',
      url: `${baseUrl}/array-ops`, method: 'POST', tags: 'builtin,data,array,list,transform',
      input_schema: { type: 'object', required: ['operation','input'], properties: { operation: { type: 'string' }, input: { type: 'array' }, args: { description: '操作參數（依 operation 而定）' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { result: {}, operation: { type: 'string' } } } } } },
    { id: 'date-ops', name: 'date-ops',
      description: '日期操作：now, format, add, subtract, diff, parse, startOf, endOf, isBefore, isAfter。',
      url: `${baseUrl}/date-ops`, method: 'POST', tags: 'builtin,data,date,time,transform',
      input_schema: { type: 'object', required: ['operation'], properties: { operation: { type: 'string' }, input: { type: 'string', description: 'ISO 日期字串（now 操作可省略）' }, args: { description: '操作參數（依 operation 而定）' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { result: {}, operation: { type: 'string' } } } } } },
    // === P4 新增：AI Transform ===
    { id: 'ai-transform-compile', name: 'ai-transform-compile',
      description: 'AI compile：輸入自然語言描述，AI 產生確定性 JS 轉換函式並儲存。返回 transform_id + 函式預覽。',
      url: `${baseUrl}/ai-transform/compile`, method: 'POST', tags: 'ai,transform,compile,nlp,codegen',
      input_schema: { type: 'object', required: ['description'], properties: { description: { type: 'string', description: '自然語言描述，如「把日期改成台灣格式 YYYY/MM/DD」' }, example_input: { description: '範例輸入（幫助 AI 理解）' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { transform_id: { type: 'string' }, fn_preview: { type: 'string' }, description: { type: 'string' } } } } } },
    { id: 'ai-transform-run', name: 'ai-transform-run',
      description: 'AI run：使用已編譯的 transform_id 機械式執行轉換（不再呼叫 AI）。',
      url: `${baseUrl}/ai-transform/run`, method: 'POST', tags: 'ai,transform,run,execute',
      input_schema: { type: 'object', required: ['transform_id','input'], properties: { transform_id: { type: 'string', description: '由 compile 端點回傳的 ID' }, input: { description: '要轉換的資料' } } },
      output_schema: { type: 'object', properties: { success: { type: 'boolean' }, data: { type: 'object', properties: { result: {}, transform_id: { type: 'string' } } } } } },
  ];
}
