window.AgixtCrudExtension.register({
  id: 'billing-admin',
  label: 'Billing Admin',
  tabs: [
    {
      id: 'stats',
      label: 'Server Stats',
      singular: 'server stats',
      endpoint: '/v1/admin/stats',
      create: false,
      update: false,
      delete: false,
      columns: [
        { key: 'total_companies', label: 'Companies' },
        { key: 'total_users', label: 'Users' },
        { key: 'total_usd_balance', label: 'USD Balance', format: 'currency' },
        { key: 'companies_no_balance', label: 'No Balance' },
        { key: 'suspended_companies', label: 'Suspended' },
      ],
      showRecordCount: false,
      summary: [
        { label: 'Companies', value: 'total_companies', tone: 'accent' },
        { label: 'Users', value: 'total_users', tone: 'accent' },
        { label: 'USD Balance', value: (rows, raw) => Number(raw && raw.total_usd_balance || 0).toLocaleString(undefined, { style: 'currency', currency: 'USD' }), tone: 'good' },
        { label: 'No Balance', value: 'companies_no_balance', tone: 'warn' },
        { label: 'Suspended', value: 'suspended_companies', tone: 'bad' },
      ],
      globalActions: [
        {
          id: 'issue-credit',
          label: 'Issue credits',
          method: 'POST',
          path: '/v1/admin/credit',
          showResult: true,
          fields: [
            { key: 'company_id', label: 'Company ID', required: true },
            { key: 'amount_usd', label: 'Amount USD', type: 'number', required: true, value: 25 },
          ],
        },
        {
          id: 'set-super-admin',
          label: 'Set super admin',
          method: 'POST',
          path: '/v1/admin/set-super-admin',
          bodyPlacement: 'query',
          showResult: true,
          fields: [
            { key: 'email', label: 'User email', type: 'email', required: true },
          ],
        },
        {
          id: 'impersonate',
          label: 'Impersonate',
          method: 'POST',
          path: '/v1/admin/impersonate',
          showResult: true,
          fields: [
            { key: 'user_email', label: 'User email', type: 'email', required: true },
          ],
        },
      ],
    },
    {
      id: 'companies',
      label: 'Companies',
      singular: 'company',
      endpoint: '/v1/admin/companies?limit=250',
      listKey: 'companies',
      idKey: 'id',
      createLabel: 'New company',
      createBodyPlacement: 'query',
      updateMethod: 'PATCH',
      updatePath: '/v1/admin/companies/{id}',
      deletePath: '/v1/admin/companies/{id}',
      searchKeys: ['name', 'email', 'id'],
      columns: [
        { key: 'name', label: 'Name' },
        { key: 'email', label: 'Email' },
        { key: 'users', label: 'Users', align: 'right', render: (value) => '<span class="dc-num">' + (Array.isArray(value) ? value.length : '0') + '</span>' },
        { key: 'token_balance_usd', label: 'USD Balance', align: 'right', format: 'currency' },
        { key: 'status', label: 'Active', format: 'bool' },
      ],
      fields: [
        { key: 'name', label: 'Name', required: true },
        { key: 'email', label: 'Email', type: 'email' },
        { key: 'phone_number', label: 'Phone' },
        { key: 'website', label: 'Website' },
        { key: 'address', label: 'Address', type: 'textarea', rows: 2 },
        { key: 'city', label: 'City' },
        { key: 'state', label: 'State' },
        { key: 'zip_code', label: 'ZIP' },
        { key: 'country', label: 'Country' },
        { key: 'notes', label: 'Admin notes', type: 'textarea', rows: 3 },
      ],
      actions: [
        { id: 'suspend', label: 'Suspend', method: 'POST', path: '/v1/admin/companies/{id}/suspend', confirm: 'Suspend this company?', danger: true },
        { id: 'unsuspend', label: 'Unsuspend', method: 'POST', path: '/v1/admin/companies/{id}/unsuspend', confirm: 'Unsuspend this company?' },
        {
          id: 'issue-credit',
          label: 'Credit',
          method: 'POST',
          path: '/v1/admin/credit',
          showResult: true,
          fields: [
            { key: 'amount_usd', label: 'Amount USD', type: 'number', required: true, value: 25 },
          ],
          body: (values, row) => ({ company_id: row.id, amount_usd: Number(values.amount_usd) }),
        },
        {
          id: 'assign-user',
          label: 'Assign user',
          method: 'POST',
          path: '/v1/admin/companies/{id}/users',
          showResult: true,
          fields: [
            { key: 'user_email', label: 'User email', type: 'email', required: true },
            { key: 'role_id', label: 'Role', type: 'select', value: 3, options: [
              { value: 0, label: 'Super Admin' },
              { value: 1, label: 'Admin' },
              { value: 2, label: 'Manager' },
              { value: 3, label: 'User' },
            ] },
          ],
        },
      ],
      globalActions: [
        {
          id: 'merge-companies',
          label: 'Merge companies',
          method: 'POST',
          path: '/v1/admin/companies/merge',
          bodyPlacement: 'query',
          danger: true,
          showResult: true,
          fields: [
            { key: 'source_company_id', label: 'Source company ID', required: true },
            { key: 'target_company_id', label: 'Target company ID', required: true },
          ],
        },
      ],
    },
    {
      id: 'export',
      label: 'Company Users',
      singular: 'company user',
      endpoint: '/v1/admin/export/companies',
      listKey: 'rows',
      create: false,
      update: false,
      delete: false,
      searchKeys: ['company_name', 'user_email', 'user_first_name', 'user_last_name', 'user_role'],
      columns: [
        { key: 'company_name', label: 'Company' },
        { key: 'user_email', label: 'User Email' },
        { key: 'user_first_name', label: 'First' },
        { key: 'user_last_name', label: 'Last' },
        { key: 'user_role', label: 'Role' },
        { key: 'company_status', label: 'Company Status' },
      ],
      actions: [
        {
          id: 'reset-password',
          label: 'Reset password',
          method: 'POST',
          path: '/v1/admin/users/{user_id}/password',
          danger: true,
          showResult: true,
          fields: [
            { key: 'new_password', label: 'New password', type: 'password', required: true },
            { key: 'confirm_password', label: 'Confirm password', type: 'password', required: true },
          ],
        },
        {
          id: 'change-role',
          label: 'Role',
          method: 'PATCH',
          path: '/v1/admin/companies/{company_id}/users/{user_id}/role',
          showResult: true,
          fields: [
            { key: 'role_id', label: 'Role', type: 'select', required: true, options: [
              { value: 0, label: 'Super Admin' },
              { value: 1, label: 'Admin' },
              { value: 2, label: 'Manager' },
              { value: 3, label: 'User' },
            ] },
          ],
        },
        { id: 'remove-company-user', label: 'Remove', method: 'DELETE', path: '/v1/admin/companies/{company_id}/users/{user_id}', confirm: 'Remove this user from the company?', danger: true, showResult: true },
        { id: 'deactivate-user', label: 'Deactivate', method: 'DELETE', path: '/v1/admin/users/{user_id}', confirm: 'Deactivate this user account?', danger: true, showResult: true },
      ],
    },
  ],
});
