window.AgixtCrudExtension.register({
  id: 'billing-analytics',
  label: 'Billing Analytics',
  tabs: [
    {
      id: 'companies',
      label: 'Companies',
      singular: 'company usage',
      endpoint: '/v1/admin/analytics/usage?limit=500',
      listKey: 'companies',
      create: false,
      update: false,
      delete: false,
      searchKeys: ['company_name', 'company_id'],
      columns: [
        { key: 'company_name', label: 'Company' },
        { key: 'user_count', label: 'Users', align: 'right' },
        { key: 'cumulative_input_tokens', label: 'Input', align: 'right', render: (v) => '<span class="dc-num">' + Number(v || 0).toLocaleString() + '</span>' },
        { key: 'cumulative_output_tokens', label: 'Output', align: 'right', render: (v) => '<span class="dc-num">' + Number(v || 0).toLocaleString() + '</span>' },
        { key: 'cumulative_total_tokens', label: 'Total', align: 'right', render: (v) => '<span class="dc-num">' + Number(v || 0).toLocaleString() + '</span>' },
        { key: 'token_balance_usd', label: 'Balance USD', align: 'right', format: 'currency' },
      ],
      summary: [
        { label: 'Input Tokens', value: (rows, raw) => Number(raw && raw.summary && raw.summary.total_input_tokens || 0).toLocaleString(), tone: 'accent' },
        { label: 'Output Tokens', value: (rows, raw) => Number(raw && raw.summary && raw.summary.total_output_tokens || 0).toLocaleString(), tone: 'accent' },
        { label: 'Total Tokens', value: (rows, raw) => Number(raw && raw.summary && raw.summary.total_tokens || 0).toLocaleString(), tone: 'good' },
        { label: 'Users', value: 'summary.total_users', tone: 'accent' },
      ],
      actions: [
        { id: 'company-detail', label: 'Usage detail', method: 'GET', path: '/v1/admin/analytics/usage/company/{company_id}', showResult: true, confirm: 'Load this company usage detail?' },
      ],
    },
    {
      id: 'users',
      label: 'Users',
      singular: 'user usage',
      endpoint: '/v1/admin/analytics/usage/users?limit=500',
      listKey: 'users',
      create: false,
      update: false,
      delete: false,
      searchKeys: ['email', 'first_name', 'last_name', 'company_name'],
      columns: [
        { key: 'email', label: 'Email' },
        { key: 'first_name', label: 'First' },
        { key: 'last_name', label: 'Last' },
        { key: 'company_name', label: 'Company' },
        { key: 'input_tokens', label: 'Input', align: 'right', render: (v) => '<span class="dc-num">' + Number(v || 0).toLocaleString() + '</span>' },
        { key: 'output_tokens', label: 'Output', align: 'right', render: (v) => '<span class="dc-num">' + Number(v || 0).toLocaleString() + '</span>' },
        { key: 'total_tokens', label: 'Total', align: 'right', render: (v) => '<span class="dc-num">' + Number(v || 0).toLocaleString() + '</span>' },
      ],
      summary: [
        { label: 'Input Tokens', value: (rows, raw) => Number(raw && raw.summary && raw.summary.total_input_tokens || 0).toLocaleString(), tone: 'accent' },
        { label: 'Output Tokens', value: (rows, raw) => Number(raw && raw.summary && raw.summary.total_output_tokens || 0).toLocaleString(), tone: 'accent' },
        { label: 'Total Tokens', value: (rows, raw) => Number(raw && raw.summary && raw.summary.total_tokens || 0).toLocaleString(), tone: 'good' },
        { label: 'Users', value: 'summary.total_users', tone: 'accent' },
      ],
    },
  ],
});
