/* global React */
// Mock data for 8.7 審核流程與條件設定

// 作用表單 — dynamically derived from SIDEBAR (items with approvable: true)
// Order and labels follow SIDEBAR exactly; no hardcoded IDs or numbers.
const APPROVAL_FORMS = SIDEBAR.flatMap(cat =>
  (cat.children || [])
    .filter(item => item.approvable)
    .map(item => ({ id: item.id, label: item.label, group: cat.label }))
);

// 條件類型 — 「無條件 / 依規則」
const COND_TYPES = [
  { id: "all",   label: "無條件觸發", hint: "表單一律進入此審核流程，不另設欄位條件。" },
  { id: "field", label: "依欄位規則", hint: "依欄位數值或變化量決定是否觸發審核。" },
];

// 規則 combinator — 多條規則之間怎麼接
const COMBINATORS = [
  { id: "and", label: "全部成立", badge: "AND", hint: "所有規則同時成立才觸發。" },
  { id: "or",  label: "任一成立", badge: "OR",  hint: "任何一條規則成立即觸發。" },
];

// 審核步驟人選類型
const STEP_TYPES = [
  { id: "role",  label: "指定職位" },
  { id: "user",  label: "指定人員" },
];

// 部門
const DEPARTMENTS = [
  { id: "all",  label: "全公司" },
  { id: "buy",  label: "採購部" },
  { id: "mkt",  label: "行銷部" },
  { id: "ops",  label: "營運部" },
];

// 職位 (Job position) options — from 8.6 Job Position
const POSITIONS = [
  "採購主管", "採購", "Oracle 匯出員", "圖檔處理員", "行銷主管", "行銷企劃",
];

// 職員 (User) options
const USERS = [
  "Johnny", "Joanna", "Neilson", "Danny", "Joy", "Amy", "Wayne",
];

// 職位 → 人員對應（1:N；用於送出時驗證步驟間審核人員是否重疊）
const POSITION_USER_MAP = {
  "採購主管":    ["Joanna"],
  "採購":        ["Neilson", "Danny"],
  "Oracle 匯出員": ["Wayne"],
  "圖檔處理員":  ["Wayne"],
  "行銷主管":    ["Amy"],
  "行銷企劃":    ["Joy"],
};

/* ========== 監看欄位 metadata — keyed by 欄位名稱 ==========
   type 決定可用 operator 與值欄位 input control：
     - number  : 數字輸入 + 單位 ；operator 為大小比較
     - percent : 數字輸入 + % ；operator 為大小比較
     - enum    : 下拉選單 (或 multi for IN/NIN)
     - text    : 文字輸入；operator 為等於/包含
*/
const AF_FIELDS = {
  "供應商編號":   { type: "text" },
  "供應商名稱":   { type: "text" },
  "進價":         { type: "number",  unit: "元" },
  "牌價":         { type: "number",  unit: "元" },
  "建議售價":     { type: "number",  unit: "元" },
  "毛利率":       { type: "percent" },
  "新毛利":       { type: "percent" },
  "舊毛利":       { type: "percent" },
  "毛利%":        { type: "percent" },
  "售價變化":     { type: "percent" },
  "進價變化":     { type: "percent" },
  "申請事項":     { type: "enum",    options: ["停售", "恢復供貨", "暫時停止供貨", "轉廠", "降階"] },
  "陳列等級":     { type: "enum",    options: ["A 級", "B 級", "C 級", "D 級"] },
  "停售原因":     { type: "enum",    options: ["無進貨", "下架", "品質"] },
  "暫停天數":     { type: "number",  unit: "天" },
  "EO 折扣率":    { type: "percent" },
  "活動類型":     { type: "enum",    options: ["深促", "淺促", "新品", "節慶"] },
  "投放區段":     { type: "enum",    options: ["全平台", "App", "Web"] },
  "活動天數":     { type: "number",  unit: "天" },
  "折扣%":        { type: "percent" },
  "庫存天數":     { type: "number",  unit: "天" },
  "門檻金額":     { type: "number",  unit: "元" },
  "折扣金額":     { type: "number",  unit: "元" },
  "上架商品數":   { type: "number",  unit: "件" },
  "組合售價":     { type: "number",  unit: "元" },
  "組合毛利%":    { type: "percent" },
  "圖片數":       { type: "number",  unit: "張" },
  "主圖類型":     { type: "enum",    options: ["商品圖", "情境圖", "包裝圖"] },
  "主圖更新時間": { type: "text" },
  "品項分級":     { type: "enum",    options: ["A 級", "B 級", "C 級"] },
};

// 規則欄位 — watchable fields by form
const WATCH_FIELDS = {
  "adj-vendor": ["供應商編號", "供應商名稱", "進價", "牌價", "毛利率"],
  "adj-price":  ["新毛利", "舊毛利", "售價變化", "牌價", "建議售價", "進價", "進價變化"],
  "adj-status": ["申請事項", "陳列等級", "停售原因", "暫停天數"],
  "adj-image":  ["圖片數", "主圖類型", "主圖更新時間"],
  "p-eats":     ["毛利%", "EO 折扣率", "建議售價", "活動天數"],
  "p-crm":      ["毛利%", "活動類型", "投放區段"],
  "p-clear":    ["折扣%", "庫存天數", "毛利%"],
  "p-coll":     ["門檻金額", "折扣金額", "毛利%"],
  "p-page":     ["上架商品數", "活動天數"],
  "p-mg":       ["組合售價", "組合毛利%"],
  "new":        ["毛利率", "進價", "牌價", "品項分級"],
};
const WATCH_FIELDS_ALL = [...new Set(Object.values(WATCH_FIELDS).flat())];

/* ========== 條件式 operators — by field type ==========
   每個 operator: { id, label, valueShape }
     valueShape: 'one'  → 單一輸入值
                 'none' → 不需填值 (有值)
*/
const OPERATORS_BY_TYPE = {
  number: [
    { id: "eq",       label: "等於",     valueShape: "one" },
    { id: "gte",      label: "大於等於", valueShape: "one" },
    { id: "lte",      label: "小於等於", valueShape: "one" },
  ],
  percent: [
    { id: "eq",       label: "等於",     valueShape: "one" },
    { id: "gte",      label: "大於等於", valueShape: "one" },
    { id: "lte",      label: "小於等於", valueShape: "one" },
  ],
  enum: [
    { id: "any",      label: "有值",     valueShape: "none" },
    { id: "is",       label: "等於",     valueShape: "one" },
    { id: "isnt",     label: "不等於",   valueShape: "one" },
  ],
  text: [
    { id: "any",      label: "有值",     valueShape: "none" },
    { id: "contains", label: "包含",     valueShape: "one" },
    { id: "ncontains",label: "不包含",   valueShape: "one" },
  ],
};

// Helper: get full operator metadata by field + opId
const getFieldType = (fieldName) => (AF_FIELDS[fieldName] || { type: "text" }).type;
const getOperators = (fieldName) => OPERATORS_BY_TYPE[getFieldType(fieldName)] || OPERATORS_BY_TYPE.text;
const getOperator  = (fieldName, opId) => getOperators(fieldName).find(o => o.id === opId) || getOperators(fieldName)[0];

/* ========== Seed flows ==========
   Schema:
     steps[i] = { type:'role'|'user', values:[string] }  // 每關單一指定人員或職位
     rules[i] = { field, op, value }                      // value 為字串
     combinator = 'and' | 'or'
     department = 'all' | 'buy' | 'mkt' | 'ops'
*/
const SEED_FLOWS = [
  // ── 2.1 商品轉廠調整 ── (1 條)
  {
    id: "AF-001",
    name: "採購主管覆核",
    formId: "adj-vendor", formLabel: "轉廠商調整",
    department: "buy",
    condType: "all",
    combinator: "and",
    steps: [{ type: "user", values: ["Johnny"] }],
    rules: [],
    enabled: true,
    created: "2026-04-01",
    updated: "2026-05-18 15:36",
  },
  // ── 2.2 商品價格調整 ──
  {
    id: "AF-005",
    name: "二階主管覆核（深紅線）",
    formId: "adj-price", formLabel: "價格調整",
    department: "buy",
    condType: "field",
    combinator: "and",
    steps: [
      { type: "role", values: ["採購主管"] },
      { type: "user", values: ["Johnny"] },
    ],
    rules: [
      { field: "新毛利", op: "lte", value: "10" },
    ],
    enabled: true,
    created: "2026-03-15",
    updated: "2026-04-10 09:21",
  },
  {
    id: "AF-002",
    name: "低毛利+大幅調價覆核",
    formId: "adj-price", formLabel: "價格調整",
    department: "buy",
    condType: "field",
    combinator: "and",
    steps: [{ type: "user", values: ["Joanna"] }],
    rules: [
      { field: "新毛利",   op: "lte", value: "20" },
      { field: "售價變化", op: "gte", value: "10" },
    ],
    enabled: true,
    created: "2026-04-01",
    updated: "2026-05-18 15:36",
  },
  {
    id: "AF-007",
    name: "一般調價（其他全部）",
    formId: "adj-price", formLabel: "價格調整",
    department: "buy",
    condType: "all",
    combinator: "and",
    steps: [{ type: "role", values: ["採購主管"] }],
    rules: [],
    enabled: true,
    created: "2026-04-05",
    updated: "2026-04-22 10:02",
  },
  // ── 2.3 商品狀態調整 ── (1 條)
  {
    id: "AF-003",
    name: "狀態變更覆核",
    formId: "adj-status", formLabel: "商品狀態調整",
    department: "buy",
    condType: "field",
    combinator: "or",
    steps: [{ type: "user", values: ["Joanna"] }],
    rules: [
      { field: "申請事項", op: "is",  value: "停售" },
      { field: "陳列等級", op: "any", value: "" },
    ],
    enabled: true,
    created: "2026-04-01",
    updated: "2026-05-18 15:26",
  },
  // ── 3.1 商品優惠總表 Eatsoffer ── (2 條)
  {
    id: "AF-004",
    name: "低毛利優惠覆核",
    formId: "p-eats", formLabel: "單品優惠（Eats Offer）",
    department: "mkt",
    condType: "field",
    combinator: "and",
    steps: [{ type: "user", values: ["Joanna"] }],
    rules: [
      { field: "毛利%", op: "lte", value: "20" },
    ],
    enabled: true,
    created: "2026-04-01",
    updated: "2026-05-18 15:26",
  },
  {
    id: "AF-008",
    name: "一般優惠（其他全部）",
    formId: "p-eats", formLabel: "單品優惠（Eats Offer）",
    department: "mkt",
    condType: "all",
    combinator: "and",
    steps: [{ type: "role", values: ["行銷企劃"] }],
    rules: [],
    enabled: true,
    created: "2026-04-08",
    updated: "2026-04-30 18:00",
  },
  // ── 3.2 商品活動總表 CRM ── (1 條，停用)
  {
    id: "AF-006",
    name: "行銷主管審核",
    formId: "p-crm", formLabel: "CRM 商品活動",
    department: "mkt",
    condType: "field",
    combinator: "and",
    steps: [{ type: "role", values: ["行銷主管"] }],
    rules: [
      { field: "活動類型", op: "is", value: "深促" },
    ],
    enabled: false,
    created: "2026-02-20",
    updated: "2026-03-02 11:10",
  },
];

Object.assign(window, {
  APPROVAL_FORMS, COND_TYPES, COMBINATORS, STEP_TYPES, POSITIONS, USERS,
  DEPARTMENTS, AF_FIELDS,
  WATCH_FIELDS, WATCH_FIELDS_ALL,
  OPERATORS_BY_TYPE, getFieldType, getOperators, getOperator,
  SEED_FLOWS, POSITION_USER_MAP,
});
