export const ALL_VALUE = "all";

export const socialBiAppOptions = [
  { label: "全部 App", value: ALL_VALUE },
  { label: "Lalu", value: "lalu" },
  { label: "Aslan", value: "aslan" },
  { label: "Yumi", value: "yumi" }
];

export const socialBiOperatorOptions = [
  { label: "全部人员", searchText: "all", value: ALL_VALUE },
  { label: "Ava Chen", searchText: "ava chen 陈", value: "ava" },
  { label: "Nora Li", searchText: "nora li 李", value: "nora" },
  { label: "Omar Saleh", searchText: "omar saleh", value: "omar" },
  { label: "Lina Gomez", searchText: "lina gomez", value: "lina" }
];

export const socialBiDepartmentOptions = [
  { label: "全部部门", value: ALL_VALUE },
  { label: "拉美运营一部", value: "latam-ops-1" },
  { label: "中东运营部", value: "mena-ops" },
  { label: "泛娱乐增长组", value: "growth" }
];

export const socialBiRegionOptions = [
  {
    children: [
      { label: "巴西", value: "br" },
      { label: "墨西哥", value: "mx" },
      { label: "哥伦比亚", value: "co" }
    ],
    label: "拉美大区",
    value: "latam"
  },
  {
    children: [
      { label: "沙特", value: "sa" },
      { label: "阿联酋", value: "ae" },
      { label: "埃及", value: "eg" }
    ],
    label: "中东大区",
    value: "mena"
  },
  {
    children: [
      { label: "印度尼西亚", value: "id" },
      { label: "菲律宾", value: "ph" }
    ],
    label: "东南亚大区",
    value: "sea"
  }
];

export const socialBiFilterDefinitions = {
  apps: { label: "App", options: socialBiAppOptions, type: "multi" },
  dateRange: { label: "日期区间", type: "dateRange" },
  departments: { label: "运营部门", options: socialBiDepartmentOptions, type: "multi" },
  operators: { label: "运营人员", options: socialBiOperatorOptions, placeholder: "搜索员工姓名", type: "searchMulti" },
  regions: { label: "地区", options: socialBiRegionOptions, type: "cascade" }
};

export const socialBiSections = [
  {
    cards: [
      { delta: 8.4, key: "recharge", label: "总充值 ($)", type: "money" },
      { delta: -2.6, key: "newUsers", label: "总新增人数", type: "number" },
      { delta: 3.8, key: "dau", label: "总日活 (DAU)", type: "number" },
      { delta: 1.7, key: "arppu", label: "综合 ARPPU ($)", type: "money" }
    ],
    columns: [
      textColumn("date", "日期"),
      textColumn("appName", "App名称"),
      numberColumn("newUsers", "新增"),
      numberColumn("dau", "日活"),
      numberColumn("paidUsers", "付费用户"),
      numberColumn("rechargeUsers", "充值用户"),
      moneyColumn("recharge", "充值 ($)"),
      moneyColumn("newUserRecharge", "新用户充值"),
      moneyColumn("coinSellerRecharge", "币商充值"),
      numberColumn("coinSellerStockCoin", "币商充值金币"),
      numberColumn("coinSellerTransferCoin", "币商出货金币"),
      moneyColumn("googleRecharge", "谷歌充值"),
      moneyColumn("thirdPartyRecharge", "三方充值"),
      flowRateColumn("gameFlowProfit", "游戏流水/利润率"),
      flowRateColumn("luckyGiftFlowProfit", "幸运礼物流水/利润率"),
      flowRateColumn("superLuckyGiftFlowProfit", "超级幸运礼物流水/利润率"),
      numberColumn("giftCoinSpent", "礼物流水"),
      numberColumn("coins", "金币数量"),
      numberColumn("consumedCoin", "消耗金币"),
      numberColumn("outputCoin", "产出金币"),
      consumeOutputColumn("consumeOutputRatio", "消耗产出比"),
      numberColumn("platformGrantCoin", "平台发放金币"),
      numberColumn("manualGrantCoin", "人工发放金币"),
      moneyColumn("salary", "存量工资 ($)"),
      numberColumn("salaryTransferCoin", "工资兑换金币"),
      durationColumn("avgMicOnlineMs", "平均麦上时间", "average"),
      durationColumn("micOnlineMs", "麦上总时长"),
      moneyColumn("arpu", "ARPU ($)", "average"),
      moneyColumn("arppu", "ARPPU ($)", "average"),
      percentColumn("paidConversionRate", "付费转化率"),
      percentColumn("rechargeConversionRate", "充值转化率"),
      moneyColumn("turnover", "App流水 ($)"),
      percentColumn("retentionD1", "次日留存"),
      percentColumn("retentionD7", "7日留存"),
      percentColumn("retentionD30", "30日留存")
    ],
    filters: ["dateRange", "apps", "operators"],
    key: "appData",
    label: "App 数据",
    rows: [
      {
        appName: "Lalu",
        arppu: 42.7,
        coins: 49120000,
        date: "2026-06-30",
        dau: 18520,
        id: "app-lalu",
        newUsers: 843,
        recharge: 284300,
        retentionD1: 31.6,
        retentionD7: 18.4,
        retentionD30: 9.8,
        salary: 62310,
        turnover: 493820
      },
      {
        appName: "Aslan",
        arppu: 39.4,
        coins: 32760000,
        date: "2026-06-30",
        dau: 12740,
        id: "app-aslan",
        newUsers: 612,
        recharge: 196740,
        retentionD1: 29.2,
        retentionD7: 16.1,
        retentionD30: 8.7,
        salary: 42180,
        turnover: 338560
      },
      {
        appName: "Yumi",
        arppu: 36.2,
        coins: 24890000,
        date: "2026-06-30",
        dau: 9240,
        id: "app-yumi",
        newUsers: 438,
        recharge: 142860,
        retentionD1: 27.9,
        retentionD7: 15.5,
        retentionD30: 7.9,
        salary: 31860,
        turnover: 251420
      }
    ],
    title: "App 数据"
  },
  {
    cards: [
      { delta: 7.3, key: "recharge", label: "部门总充值 ($)", type: "money" },
      { delta: 4.8, key: "turnover", label: "部门总流水 ($)", type: "money" },
      { delta: -1.8, key: "newUsers", label: "部门总新增", type: "number" },
      { delta: 2.9, key: "dau", label: "部门总日活 (DAU)", type: "number" }
    ],
    columns: [
      textColumn("date", "日期"),
      textColumn("department", "部门"),
      textColumn("appName", "App名称"),
      textColumn("region", "负责地区"),
      moneyColumn("recharge", "充值 ($)"),
      numberColumn("newUsers", "新增"),
      progressColumn("dayProgress", "当日目标完成进度"),
      progressColumn("monthProgress", "当月目标完成进度")
    ],
    filters: ["dateRange", "apps", "departments"],
    key: "department",
    label: "部门看板",
    rows: [
      {
        appName: "Lalu",
        date: "2026-06-30",
        dau: 14220,
        dayProgress: progress(126800, 140000),
        department: "拉美运营一部",
        id: "dept-latam",
        monthProgress: progress(3020000, 3600000),
        newUsers: 516,
        recharge: 126800,
        region: "拉美大区",
        turnover: 225400
      },
      {
        appName: "Aslan",
        date: "2026-06-30",
        dau: 10540,
        dayProgress: progress(96800, 90000),
        department: "中东运营部",
        id: "dept-mena",
        monthProgress: progress(2416000, 2700000),
        newUsers: 452,
        recharge: 96800,
        region: "中东大区",
        turnover: 164300
      },
      {
        appName: "Yumi",
        date: "2026-06-30",
        dau: 8160,
        dayProgress: noGoal(),
        department: "泛娱乐增长组",
        id: "dept-growth",
        monthProgress: progress(1682000, 2100000),
        newUsers: 372,
        recharge: 74200,
        region: "东南亚大区",
        turnover: 118900
      }
    ],
    title: "部门看板"
  },
  {
    cards: [
      { delta: 6.5, key: "recharge", label: "人员充值合计 ($)", type: "money" },
      { delta: 4.4, key: "turnover", label: "人员流水合计 ($)", type: "money" },
      { delta: 2.1, key: "newUsers", label: "新增人数", type: "number" },
      { delta: -3.2, key: "monthKpiRate", label: "当月充值 KPI 达成率 (%)", type: "percent" }
    ],
    columns: [
      textColumn("date", "日期"),
      textColumn("operator", "运营人员"),
      textColumn("appName", "App名称"),
      textColumn("region", "所管地区"),
      moneyColumn("recharge", "充值 ($)"),
      numberColumn("newUsers", "新增"),
      progressColumn("dayProgress", "当日目标完成进度"),
      progressColumn("monthProgress", "当月目标完成进度")
    ],
    filters: ["dateRange", "apps", "operators"],
    key: "personnel",
    label: "人员绩效",
    rows: [
      {
        appName: "Lalu",
        date: "2026-06-30",
        dayProgress: progress(52800, 50000),
        id: "person-ava",
        monthKpiRate: 88.4,
        monthProgress: progress(1182000, 1350000),
        newUsers: 211,
        operator: "Ava Chen",
        recharge: 52800,
        region: "巴西 / 墨西哥",
        turnover: 94100
      },
      {
        appName: "Aslan",
        date: "2026-06-30",
        dayProgress: progress(41600, 45000),
        id: "person-omar",
        monthKpiRate: 82.1,
        monthProgress: progress(906000, 1100000),
        newUsers: 186,
        operator: "Omar Saleh",
        recharge: 41600,
        region: "沙特 / 阿联酋",
        turnover: 70400
      },
      {
        appName: "Yumi",
        date: "2026-06-30",
        dayProgress: noGoal(),
        id: "person-lina",
        monthKpiRate: 75.6,
        monthProgress: progress(642000, 850000),
        newUsers: 154,
        operator: "Lina Gomez",
        recharge: 32600,
        region: "菲律宾",
        turnover: 52900
      }
    ],
    title: "人员绩效"
  },
  {
    cards: [
      { delta: 9.2, key: "recharge", label: "区域总充值 ($)", type: "money" },
      { delta: -1.1, key: "newUsers", label: "区域总新增", type: "number" },
      { delta: 2.4, key: "arppu", label: "区域综合 ARPPU ($)", type: "money" }
    ],
    columns: [
      textColumn("date", "日期"),
      textColumn("region", "地区"),
      textColumn("appName", "App名称"),
      moneyColumn("recharge", "充值 ($)"),
      moneyColumn("turnover", "流水 ($)"),
      moneyColumn("arppu", "ARPPU ($)", "average"),
      numberColumn("newUsers", "新增"),
      numberColumn("dau", "日活"),
      percentColumn("retentionD1", "次留"),
      percentColumn("retentionD7", "7日留"),
      percentColumn("retentionD30", "30日留"),
      moneyColumn("salary", "存量工资 ($)"),
      numberColumn("coins", "金币数量"),
      ratioColumn("agencyCount", "Agency数量"),
      ratioColumn("hostCount", "Host数量"),
      ratioColumn("bdCount", "BD数量"),
      ratioColumn("coinSellerCount", "币商数量"),
      numberColumn("coinSellerCoins", "币商金币数")
    ],
    filters: ["dateRange", "apps", "regions"],
    key: "regional",
    label: "地区分析",
    rows: [
      {
        agencyCount: ratioCount(84, 112),
        appName: "Lalu",
        arppu: 44.1,
        bdCount: ratioCount(26, 34),
        coinSellerCoins: 9840000,
        coinSellerCount: ratioCount(18, 24),
        coins: 36120000,
        date: "2026-06-30",
        dau: 13280,
        hostCount: ratioCount(412, 520),
        id: "region-br",
        newUsers: 506,
        recharge: 206400,
        region: "巴西",
        retentionD1: 32.8,
        retentionD7: 19.4,
        retentionD30: 10.2,
        salary: 52200,
        turnover: 354200
      },
      {
        agencyCount: ratioCount(66, 95),
        appName: "Aslan",
        arppu: 41.5,
        bdCount: ratioCount(19, 28),
        coinSellerCoins: 7420000,
        coinSellerCount: ratioCount(15, 21),
        coins: 28450000,
        date: "2026-06-30",
        dau: 11140,
        hostCount: ratioCount(356, 480),
        id: "region-sa",
        newUsers: 442,
        recharge: 173200,
        region: "沙特",
        retentionD1: 30.4,
        retentionD7: 17.7,
        retentionD30: 9.1,
        salary: 44100,
        turnover: 292600
      },
      {
        agencyCount: ratioCount(41, 68),
        appName: "Yumi",
        arppu: 34.8,
        bdCount: ratioCount(14, 22),
        coinSellerCoins: 4180000,
        coinSellerCount: ratioCount(9, 17),
        coins: 17260000,
        date: "2026-06-30",
        dau: 6840,
        hostCount: ratioCount(228, 346),
        id: "region-ph",
        newUsers: 318,
        recharge: 94800,
        region: "菲律宾",
        retentionD1: 27.5,
        retentionD7: 14.9,
        retentionD30: 7.3,
        salary: 22600,
        turnover: 164900
      }
    ],
    title: "地区分析"
  },
  {
    cards: [
      { delta: -1.7, key: "newUsers", label: "总新增", type: "number" },
      { delta: 3.5, key: "dau", label: "总日活", type: "number" },
      { delta: 1.2, key: "retentionD1", label: "次日留存均值 (%)", type: "percent" },
      { delta: 4.1, key: "ecosystemActive", label: "生态角色总活跃数", type: "number" }
    ],
    columns: [
      textColumn("date", "日期"),
      textColumn("region", "地区"),
      textColumn("appName", "App名称"),
      numberColumn("newUsers", "新增"),
      numberColumn("dau", "日活 (DAU)"),
      percentColumn("retentionD1", "次日留存"),
      percentColumn("retentionD7", "7日留存"),
      percentColumn("retentionD30", "30日留存"),
      ratioColumn("agencyActive", "Agency活跃数量"),
      ratioColumn("hostActive", "Host活跃数量"),
      ratioColumn("bdActive", "BD活跃数量"),
      ratioColumn("coinSellerActive", "币商活跃数量")
    ],
    filters: ["dateRange", "apps", "regions"],
    key: "retention",
    label: "留存分析",
    rows: [
      {
        agencyActive: ratioCount(84, 112),
        appName: "Lalu",
        bdActive: ratioCount(26, 34),
        coinSellerActive: ratioCount(18, 24),
        date: "2026-06-30",
        dau: 13280,
        ecosystemActive: 540,
        hostActive: ratioCount(412, 520),
        id: "retain-br",
        newUsers: 506,
        region: "巴西",
        retentionD1: 32.8,
        retentionD7: 19.4,
        retentionD30: 10.2
      },
      {
        agencyActive: ratioCount(66, 95),
        appName: "Aslan",
        bdActive: ratioCount(19, 28),
        coinSellerActive: ratioCount(15, 21),
        date: "2026-06-30",
        dau: 11140,
        ecosystemActive: 456,
        hostActive: ratioCount(356, 480),
        id: "retain-sa",
        newUsers: 442,
        region: "沙特",
        retentionD1: 30.4,
        retentionD7: 17.7,
        retentionD30: 9.1
      },
      {
        agencyActive: ratioCount(41, 68),
        appName: "Yumi",
        bdActive: ratioCount(14, 22),
        coinSellerActive: ratioCount(9, 17),
        date: "2026-06-30",
        dau: 6840,
        ecosystemActive: 292,
        hostActive: ratioCount(228, 346),
        id: "retain-ph",
        newUsers: 318,
        region: "菲律宾",
        retentionD1: 27.5,
        retentionD7: 14.9,
        retentionD30: 7.3
      }
    ],
    title: "留存分析"
  }
];

function textColumn(key, label) {
  return { aggregate: "text", align: "left", key, label, type: "text" };
}

function moneyColumn(key, label, aggregate = "sum") {
  return { aggregate, align: "right", key, label, type: "money" };
}

function numberColumn(key, label) {
  return { aggregate: "sum", align: "right", key, label, type: "number" };
}

function percentColumn(key, label) {
  return { aggregate: "average", align: "right", key, label, type: "percent" };
}

function progressColumn(key, label) {
  return { aggregate: "progress", align: "left", key, label, type: "progress" };
}

function ratioColumn(key, label) {
  return { aggregate: "ratio", align: "right", key, label, type: "ratio" };
}

function flowRateColumn(key, label) {
  return { aggregate: "flowRate", align: "right", key, label, type: "flowRate" };
}

function consumeOutputColumn(key, label) {
  return { aggregate: "consumeOutput", align: "right", key, label, type: "consumeOutput" };
}

function durationColumn(key, label, aggregate = "sum") {
  return { aggregate: aggregate === "average" ? "durationAverage" : "duration", align: "right", key, label, type: "duration" };
}

function progress(actual, target) {
  return { actual, target };
}

function noGoal() {
  return { actual: 0, noGoal: true, target: 0 };
}

function ratioCount(active, total) {
  return { active, total };
}
