import { APIResource } from '../../../../core/resource';
import * as AnalyticsAPI from './analytics';
import { BetaAnalyticsPluginActivitiesPageCursor } from './analytics';
import { PageCursor, type PageCursorParams, PagePromise } from '../../../../core/pagination';
import { RequestOptions } from '../../../../internal/request-options';
import { startApiCallSpan } from '../../../../internal/tracing';

export class Plugins extends APIResource {
  /**
   * Get per-plugin install + invocation usage for a given day, with pagination.
   *
   * Returns plugin usage metrics for the organization across Cowork and Claude Code,
   * sorted by plugin name. The `plugin_name` value `third-party` is an aggregate
   * bucket, not a plugin: it collects plugin activity, from either surface, for
   * which the reporting client did not provide a plugin name — so an organization's
   * own plugins can contribute both to their own named rows and to this bucket. Use
   * `group_by[]` to break usage out per member, per RBAC group, or per product
   * surface (Cowork / Claude Code), and `filter[]` to scope results; the parameter
   * descriptions list the supported dimensions. Requires an API key with the
   * `read:analytics` scope. `starting_date` / `ending_date` select range-rollup mode
   * like `/skills`.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const betaAnalyticsPluginActivity of client.beta.organization.analytics.plugins.list()) {
   *   // ...
   * }
   * ```
   */
  list(
    query: PluginListParams | null | undefined = {},
    options?: RequestOptions,
  ): PagePromise<BetaAnalyticsPluginActivitiesPageCursor, AnalyticsAPI.BetaAnalyticsPluginActivity> {
    return this._client.getAPIList(
      '/v1/organizations/analytics/plugins?beta=true',
      PageCursor<AnalyticsAPI.BetaAnalyticsPluginActivity>,
      {
        query,
        ...options,
        __span: startApiCallSpan(this._client, 'anthropic.organization.analytics.plugins.list', options, {
          route: '/v1/organizations/analytics/plugins',
          queryParams: [
            'date',
            'ending_date',
            'filter',
            'group_by',
            'limit',
            'order',
            'order_by',
            'page',
            'starting_date',
          ],
          contentQueryParams: ['filter'],
        }),
      },
    );
  }
}

export interface PluginListParams extends PageCursorParams {
  /**
   * UTC date in YYYY-MM-DD format. The day to get plugin usage for. Data is
   * typically available with a 1-day lag (varies by query; the error for a
   * too-recent date names the latest available day) and may be revised by a few
   * percent over the following days. No earlier than 2026-01-01.
   */
  date?: string | null;

  /**
   * UTC date in YYYY-MM-DD format. End of the date range (exclusive); only valid
   * with `starting_date`. Data is typically available with a 1-day lag (varies by
   * query; the error for a too-recent date names the latest available day), so this
   * can be at most today — which is also the default when omitted, resolved once
   * when the first page is served and reused for the rest of the pagination
   * sequence. At most 366 days after `starting_date`.
   */
  ending_date?: string | null;

  /**
   * Filters as `dimension:value`, e.g. `filter[]=rbac_group_id:{id}`. Repeat the
   * param for OR within a dimension and across dimensions for AND. Supported
   * dimensions on this endpoint: `plugin_name`, `product`, `rbac_group_id`,
   * `user_id`. Value forms: `plugin_name` matches case-insensitively; `product` is
   * `chat_cowork_unified`, `claude_code`, or `cowork` (the only surfaces with plugin
   * attribution); `rbac_group_id` takes the tagged id (`rbac_group_...`, as emitted
   * in responses and by the spend-limits API) or a bare group UUID, and matches
   * users who held the group at any point during each covered UTC day (time-of-usage
   * attribution); `user_id` takes a tagged user id (`user_...`), as emitted in
   * responses. An unsupported dimension returns 400. At most 100 entries.
   * `chat_cowork_unified` is accepted as a `product` value only on deployments that
   * offer Chat and Cowork unified.
   */
  filter?: Array<string> | null;

  /**
   * Dimensions to break results out by (e.g. `group_by[]=user_id`). Supported on
   * this endpoint: `product`, `rbac_group_id`, `user_id`. On this endpoint `product`
   * takes the values `chat_cowork_unified`, `claude_code`, or `cowork` only (the
   * surfaces with plugin attribution). Grouped rows carry the requested dimension
   * values as additional fields and paginate like ungrouped responses via
   * `next_page`; an unsupported dimension returns 400. `rbac_group_id` attributes a
   * user to every group they held at any point during each covered UTC day, so
   * grouped rows are not an exclusive partition and can sum above org-level totals.
   * At most 100 entries.
   */
  group_by?: Array<'product' | 'rbac_group_id' | 'user_id'> | null;

  /**
   * Sort direction: `asc` or `desc`. Defaults to `asc` for the endpoint's sort
   * column and to `desc` when `order_by` names a metric (a top-N ranking). Applies
   * to `order_by`, or to the endpoint's default sort field when `order_by` is
   * omitted.
   */
  order?: 'asc' | 'desc' | null;

  /**
   * Sort field. Restricted to the endpoint's sort column plus its rankable metrics
   * (metrics default to descending; a few metrics rank in date-range mode only, per
   * the endpoint's documented orderable set).
   */
  order_by?: string | null;

  /**
   * UTC date in YYYY-MM-DD format. Start of a date range (inclusive). Enables rollup
   * mode: one row per entity aggregated over the whole range — addable counters are
   * summed across days, and a distinct count is never summed where summing could
   * double-count (a field's range value is recomputed exactly over the window,
   * approximate via HLL with typical error under 2%, null, or — for the
   * creation-event counts, whose per-day values cannot overlap — a per-day sum that
   * is itself exact; each field's own description says which). Use either `date` or
   * `starting_date`, not both. Data is typically available with a 1-day lag (varies
   * by query; the error for a too-recent date names the latest available day) and
   * may be revised by a few percent over the following days. No earlier than
   * 2026-01-01.
   */
  starting_date?: string | null;
}

export declare namespace Plugins {
  export { type PluginListParams as PluginListParams };
}

export { type BetaAnalyticsPluginActivitiesPageCursor };
