/* tslint:disable */
/* eslint-disable */
/**
 * AutoGlue API
 * API for managing K3s clusters across cloud providers
 *
 * The version of the OpenAPI document: dev
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

import * as runtime from "../runtime";
import type {
  HandlersMemberOut,
  HandlersMemberUpsertReq,
  HandlersOrgCreateReq,
  HandlersOrgKeyCreateReq,
  HandlersOrgKeyCreateResp,
  HandlersOrgUpdateReq,
  ModelsAPIKey,
  ModelsOrganization,
  UtilsErrorResponse,
} from "../models/index";
import {
  HandlersMemberOutFromJSON,
  HandlersMemberOutToJSON,
  HandlersMemberUpsertReqFromJSON,
  HandlersMemberUpsertReqToJSON,
  HandlersOrgCreateReqFromJSON,
  HandlersOrgCreateReqToJSON,
  HandlersOrgKeyCreateReqFromJSON,
  HandlersOrgKeyCreateReqToJSON,
  HandlersOrgKeyCreateRespFromJSON,
  HandlersOrgKeyCreateRespToJSON,
  HandlersOrgUpdateReqFromJSON,
  HandlersOrgUpdateReqToJSON,
  ModelsAPIKeyFromJSON,
  ModelsAPIKeyToJSON,
  ModelsOrganizationFromJSON,
  ModelsOrganizationToJSON,
  UtilsErrorResponseFromJSON,
  UtilsErrorResponseToJSON,
} from "../models/index";

export interface AddOrUpdateMemberRequest {
  id: string;
  handlersMemberUpsertReq: HandlersMemberUpsertReq;
}

export interface CreateOrgRequest {
  handlersOrgCreateReq: HandlersOrgCreateReq;
}

export interface CreateOrgKeyRequest {
  id: string;
  handlersOrgKeyCreateReq: HandlersOrgKeyCreateReq;
}

export interface DeleteOrgRequest {
  id: string;
}

export interface DeleteOrgKeyRequest {
  id: string;
  keyId: string;
}

export interface GetOrgRequest {
  id: string;
}

export interface ListMembersRequest {
  id: string;
}

export interface ListOrgKeysRequest {
  id: string;
}

export interface RemoveMemberRequest {
  id: string;
  userId: string;
}

export interface UpdateOrgRequest {
  id: string;
  handlersOrgUpdateReq: HandlersOrgUpdateReq;
}

/**
 *
 */
export class OrgsApi extends runtime.BaseAPI {
  /**
   * Add or update a member (owner/admin)
   */
  async addOrUpdateMemberRaw(
    requestParameters: AddOrUpdateMemberRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<HandlersMemberOut>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling addOrUpdateMember().',
      );
    }

    if (requestParameters["handlersMemberUpsertReq"] == null) {
      throw new runtime.RequiredError(
        "handlersMemberUpsertReq",
        'Required parameter "handlersMemberUpsertReq" was null or undefined when calling addOrUpdateMember().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    headerParameters["Content-Type"] = "application/json";

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}/members`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "POST",
        headers: headerParameters,
        query: queryParameters,
        body: HandlersMemberUpsertReqToJSON(
          requestParameters["handlersMemberUpsertReq"],
        ),
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      HandlersMemberOutFromJSON(jsonValue),
    );
  }

  /**
   * Add or update a member (owner/admin)
   */
  async addOrUpdateMember(
    requestParameters: AddOrUpdateMemberRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<HandlersMemberOut> {
    const response = await this.addOrUpdateMemberRaw(
      requestParameters,
      initOverrides,
    );
    return await response.value();
  }

  /**
   * Create organization
   */
  async createOrgRaw(
    requestParameters: CreateOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<ModelsOrganization>> {
    if (requestParameters["handlersOrgCreateReq"] == null) {
      throw new runtime.RequiredError(
        "handlersOrgCreateReq",
        'Required parameter "handlersOrgCreateReq" was null or undefined when calling createOrg().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    headerParameters["Content-Type"] = "application/json";

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs`;

    const response = await this.request(
      {
        path: urlPath,
        method: "POST",
        headers: headerParameters,
        query: queryParameters,
        body: HandlersOrgCreateReqToJSON(
          requestParameters["handlersOrgCreateReq"],
        ),
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      ModelsOrganizationFromJSON(jsonValue),
    );
  }

  /**
   * Create organization
   */
  async createOrg(
    requestParameters: CreateOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<ModelsOrganization> {
    const response = await this.createOrgRaw(requestParameters, initOverrides);
    return await response.value();
  }

  /**
   * Create org key/secret pair (owner/admin)
   */
  async createOrgKeyRaw(
    requestParameters: CreateOrgKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<HandlersOrgKeyCreateResp>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling createOrgKey().',
      );
    }

    if (requestParameters["handlersOrgKeyCreateReq"] == null) {
      throw new runtime.RequiredError(
        "handlersOrgKeyCreateReq",
        'Required parameter "handlersOrgKeyCreateReq" was null or undefined when calling createOrgKey().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    headerParameters["Content-Type"] = "application/json";

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}/api-keys`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "POST",
        headers: headerParameters,
        query: queryParameters,
        body: HandlersOrgKeyCreateReqToJSON(
          requestParameters["handlersOrgKeyCreateReq"],
        ),
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      HandlersOrgKeyCreateRespFromJSON(jsonValue),
    );
  }

  /**
   * Create org key/secret pair (owner/admin)
   */
  async createOrgKey(
    requestParameters: CreateOrgKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<HandlersOrgKeyCreateResp> {
    const response = await this.createOrgKeyRaw(
      requestParameters,
      initOverrides,
    );
    return await response.value();
  }

  /**
   * Delete organization (owner)
   */
  async deleteOrgRaw(
    requestParameters: DeleteOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<void>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling deleteOrg().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "DELETE",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.VoidApiResponse(response);
  }

  /**
   * Delete organization (owner)
   */
  async deleteOrg(
    requestParameters: DeleteOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<void> {
    await this.deleteOrgRaw(requestParameters, initOverrides);
  }

  /**
   * Delete org key (owner/admin)
   */
  async deleteOrgKeyRaw(
    requestParameters: DeleteOrgKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<void>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling deleteOrgKey().',
      );
    }

    if (requestParameters["keyId"] == null) {
      throw new runtime.RequiredError(
        "keyId",
        'Required parameter "keyId" was null or undefined when calling deleteOrgKey().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}/api-keys/{key_id}`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );
    urlPath = urlPath.replace(
      `{${"key_id"}}`,
      encodeURIComponent(String(requestParameters["keyId"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "DELETE",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.VoidApiResponse(response);
  }

  /**
   * Delete org key (owner/admin)
   */
  async deleteOrgKey(
    requestParameters: DeleteOrgKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<void> {
    await this.deleteOrgKeyRaw(requestParameters, initOverrides);
  }

  /**
   * Get organization
   */
  async getOrgRaw(
    requestParameters: GetOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<ModelsOrganization>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling getOrg().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "GET",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      ModelsOrganizationFromJSON(jsonValue),
    );
  }

  /**
   * Get organization
   */
  async getOrg(
    requestParameters: GetOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<ModelsOrganization> {
    const response = await this.getOrgRaw(requestParameters, initOverrides);
    return await response.value();
  }

  /**
   * List members in org
   */
  async listMembersRaw(
    requestParameters: ListMembersRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<Array<HandlersMemberOut>>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling listMembers().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}/members`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "GET",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      jsonValue.map(HandlersMemberOutFromJSON),
    );
  }

  /**
   * List members in org
   */
  async listMembers(
    requestParameters: ListMembersRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<Array<HandlersMemberOut>> {
    const response = await this.listMembersRaw(
      requestParameters,
      initOverrides,
    );
    return await response.value();
  }

  /**
   * List organizations I belong to
   */
  async listMyOrgsRaw(
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<Array<ModelsOrganization>>> {
    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs`;

    const response = await this.request(
      {
        path: urlPath,
        method: "GET",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      jsonValue.map(ModelsOrganizationFromJSON),
    );
  }

  /**
   * List organizations I belong to
   */
  async listMyOrgs(
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<Array<ModelsOrganization>> {
    const response = await this.listMyOrgsRaw(initOverrides);
    return await response.value();
  }

  /**
   * List org-scoped API keys (no secrets)
   */
  async listOrgKeysRaw(
    requestParameters: ListOrgKeysRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<Array<ModelsAPIKey>>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling listOrgKeys().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}/api-keys`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "GET",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      jsonValue.map(ModelsAPIKeyFromJSON),
    );
  }

  /**
   * List org-scoped API keys (no secrets)
   */
  async listOrgKeys(
    requestParameters: ListOrgKeysRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<Array<ModelsAPIKey>> {
    const response = await this.listOrgKeysRaw(
      requestParameters,
      initOverrides,
    );
    return await response.value();
  }

  /**
   * Remove a member (owner/admin)
   */
  async removeMemberRaw(
    requestParameters: RemoveMemberRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<void>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling removeMember().',
      );
    }

    if (requestParameters["userId"] == null) {
      throw new runtime.RequiredError(
        "userId",
        'Required parameter "userId" was null or undefined when calling removeMember().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}/members/{user_id}`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );
    urlPath = urlPath.replace(
      `{${"user_id"}}`,
      encodeURIComponent(String(requestParameters["userId"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "DELETE",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.VoidApiResponse(response);
  }

  /**
   * Remove a member (owner/admin)
   */
  async removeMember(
    requestParameters: RemoveMemberRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<void> {
    await this.removeMemberRaw(requestParameters, initOverrides);
  }

  /**
   * Update organization (owner/admin)
   */
  async updateOrgRaw(
    requestParameters: UpdateOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<ModelsOrganization>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling updateOrg().',
      );
    }

    if (requestParameters["handlersOrgUpdateReq"] == null) {
      throw new runtime.RequiredError(
        "handlersOrgUpdateReq",
        'Required parameter "handlersOrgUpdateReq" was null or undefined when calling updateOrg().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    headerParameters["Content-Type"] = "application/json";

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/orgs/{id}`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "PATCH",
        headers: headerParameters,
        query: queryParameters,
        body: HandlersOrgUpdateReqToJSON(
          requestParameters["handlersOrgUpdateReq"],
        ),
      },
      initOverrides,
    );

    return new runtime.JSONApiResponse(response, (jsonValue) =>
      ModelsOrganizationFromJSON(jsonValue),
    );
  }

  /**
   * Update organization (owner/admin)
   */
  async updateOrg(
    requestParameters: UpdateOrgRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<ModelsOrganization> {
    const response = await this.updateOrgRaw(requestParameters, initOverrides);
    return await response.value();
  }
}
