import {
  BaseMetorialEndpoint,
  MetorialEndpointManager
} from '@metorial/util-endpoint';

import {
  mapDashboardInstancePortalsConsumerGroupsCreateBody,
  mapDashboardInstancePortalsConsumerGroupsCreateOutput,
  mapDashboardInstancePortalsConsumerGroupsDeleteOutput,
  mapDashboardInstancePortalsConsumerGroupsGetOutput,
  mapDashboardInstancePortalsConsumerGroupsListOutput,
  mapDashboardInstancePortalsConsumerGroupsListQuery,
  mapDashboardInstancePortalsConsumerGroupsUpdateBody,
  mapDashboardInstancePortalsConsumerGroupsUpdateOutput,
  type DashboardInstancePortalsConsumerGroupsCreateBody,
  type DashboardInstancePortalsConsumerGroupsCreateOutput,
  type DashboardInstancePortalsConsumerGroupsDeleteOutput,
  type DashboardInstancePortalsConsumerGroupsGetOutput,
  type DashboardInstancePortalsConsumerGroupsListOutput,
  type DashboardInstancePortalsConsumerGroupsListQuery,
  type DashboardInstancePortalsConsumerGroupsUpdateBody,
  type DashboardInstancePortalsConsumerGroupsUpdateOutput
} from '../resources';

/**
 * @name Portal Consumer Groups controller
 * @description Manage the consumer groups that drive portal visibility and access rules.
 *
 * @see https://metorial.com/api
 * @see https://metorial.com/docs
 */
export class MetorialDashboardInstancePortalsConsumerGroupsEndpoint {
  constructor(private readonly _manager: MetorialEndpointManager<any>) {}

  // thin proxies so method bodies stay unchanged
  private _get(request: any) {
    return this._manager._get(request);
  }
  private _post(request: any) {
    return this._manager._post(request);
  }
  private _put(request: any) {
    return this._manager._put(request);
  }
  private _patch(request: any) {
    return this._manager._patch(request);
  }
  private _delete(request: any) {
    return this._manager._delete(request);
  }

  /**
   * @name List portal consumer groups
   * @description Returns a paginated list of consumer groups for a portal.
   *
   * @param `instanceId` - string
   * @param `portalId` - string
   * @param `query` - DashboardInstancePortalsConsumerGroupsListQuery
   * @param `opts` - { headers?: Record<string, string> }
   * @returns DashboardInstancePortalsConsumerGroupsListOutput
   * @see https://metorial.com/api
   * @see https://metorial.com/docs
   */
  list(
    instanceId: string,
    portalId: string,
    query?: DashboardInstancePortalsConsumerGroupsListQuery,
    opts?: { headers?: Record<string, string> }
  ): Promise<DashboardInstancePortalsConsumerGroupsListOutput> {
    let path = `dashboard/instances/${instanceId}/portals/${portalId}/consumer-groups`;

    let request = {
      path,

      query: query
        ? mapDashboardInstancePortalsConsumerGroupsListQuery.transformTo(query)
        : undefined,
      ...(opts?.headers ? { headers: opts.headers } : {})
    } as any;

    return this._get(request).transform(
      mapDashboardInstancePortalsConsumerGroupsListOutput
    );
  }

  /**
   * @name Get portal consumer group
   * @description Retrieves a portal consumer group by ID.
   *
   * @param `instanceId` - string
   * @param `portalId` - string
   * @param `consumerGroupId` - string
   * @param `opts` - { headers?: Record<string, string> }
   * @returns DashboardInstancePortalsConsumerGroupsGetOutput
   * @see https://metorial.com/api
   * @see https://metorial.com/docs
   */
  get(
    instanceId: string,
    portalId: string,
    consumerGroupId: string,
    opts?: { headers?: Record<string, string> }
  ): Promise<DashboardInstancePortalsConsumerGroupsGetOutput> {
    let path = `dashboard/instances/${instanceId}/portals/${portalId}/consumer-groups/${consumerGroupId}`;

    let request = {
      path,

      ...(opts?.headers ? { headers: opts.headers } : {})
    } as any;

    return this._get(request).transform(
      mapDashboardInstancePortalsConsumerGroupsGetOutput
    );
  }

  /**
   * @name Create portal consumer group
   * @description Creates a new consumer group for the portal.
   *
   * @param `instanceId` - string
   * @param `portalId` - string
   * @param `body` - DashboardInstancePortalsConsumerGroupsCreateBody
   * @param `opts` - { headers?: Record<string, string> }
   * @returns DashboardInstancePortalsConsumerGroupsCreateOutput
   * @see https://metorial.com/api
   * @see https://metorial.com/docs
   */
  create(
    instanceId: string,
    portalId: string,
    body: DashboardInstancePortalsConsumerGroupsCreateBody,
    opts?: { headers?: Record<string, string> }
  ): Promise<DashboardInstancePortalsConsumerGroupsCreateOutput> {
    let path = `dashboard/instances/${instanceId}/portals/${portalId}/consumer-groups`;

    let request = {
      path,
      body: mapDashboardInstancePortalsConsumerGroupsCreateBody.transformTo(
        body
      ),

      ...(opts?.headers ? { headers: opts.headers } : {})
    } as any;

    return this._post(request).transform(
      mapDashboardInstancePortalsConsumerGroupsCreateOutput
    );
  }

  /**
   * @name Update portal consumer group
   * @description Updates a consumer group for the portal.
   *
   * @param `instanceId` - string
   * @param `portalId` - string
   * @param `consumerGroupId` - string
   * @param `body` - DashboardInstancePortalsConsumerGroupsUpdateBody
   * @param `opts` - { headers?: Record<string, string> }
   * @returns DashboardInstancePortalsConsumerGroupsUpdateOutput
   * @see https://metorial.com/api
   * @see https://metorial.com/docs
   */
  update(
    instanceId: string,
    portalId: string,
    consumerGroupId: string,
    body: DashboardInstancePortalsConsumerGroupsUpdateBody,
    opts?: { headers?: Record<string, string> }
  ): Promise<DashboardInstancePortalsConsumerGroupsUpdateOutput> {
    let path = `dashboard/instances/${instanceId}/portals/${portalId}/consumer-groups/${consumerGroupId}`;

    let request = {
      path,
      body: mapDashboardInstancePortalsConsumerGroupsUpdateBody.transformTo(
        body
      ),

      ...(opts?.headers ? { headers: opts.headers } : {})
    } as any;

    return this._patch(request).transform(
      mapDashboardInstancePortalsConsumerGroupsUpdateOutput
    );
  }

  /**
   * @name Delete portal consumer group
   * @description Archives a consumer group for the portal.
   *
   * @param `instanceId` - string
   * @param `portalId` - string
   * @param `consumerGroupId` - string
   * @param `opts` - { headers?: Record<string, string> }
   * @returns DashboardInstancePortalsConsumerGroupsDeleteOutput
   * @see https://metorial.com/api
   * @see https://metorial.com/docs
   */
  delete(
    instanceId: string,
    portalId: string,
    consumerGroupId: string,
    opts?: { headers?: Record<string, string> }
  ): Promise<DashboardInstancePortalsConsumerGroupsDeleteOutput> {
    let path = `dashboard/instances/${instanceId}/portals/${portalId}/consumer-groups/${consumerGroupId}`;

    let request = {
      path,

      ...(opts?.headers ? { headers: opts.headers } : {})
    } as any;

    return this._delete(request).transform(
      mapDashboardInstancePortalsConsumerGroupsDeleteOutput
    );
  }
}
