import * as plugins from './plugins.js';
import type { CloudlyApiClient } from './classes.cloudlyapiclient.js';

export class SecretGroup implements plugins.servezoneInterfaces.data.ISecretGroup {
  public cloudlyClientRef: CloudlyApiClient;
  
  public id: string;
  public data: plugins.servezoneInterfaces.data.ISecretGroup['data'];

  constructor(cloudlyClientRef: CloudlyApiClient) {
    this.cloudlyClientRef = cloudlyClientRef;
  }

  public static async getSecretGroupById(cloudlyClientRef: CloudlyApiClient, secretGroupIdArg: string) {
    const getSecretGroupByIdTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.secretgroup.IReq_GetSecretGroupById>(
      'getSecretGroupById'
    );
    const response = await getSecretGroupByIdTR.fire({
      identity: cloudlyClientRef.identity,
      secretGroupId: secretGroupIdArg,
    });
    const newSecretGroup = new SecretGroup(cloudlyClientRef);
    Object.assign(newSecretGroup, response.secretGroup);
    return newSecretGroup;
  }

  public static async getSecretGroups(cloudlyClientRef: CloudlyApiClient) {
    const getSecretGroupsTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.secretgroup.IReq_GetSecretGroups>(
      'getSecretGroups'
    );
    const response = await getSecretGroupsTR.fire({
      identity: cloudlyClientRef.identity,
    });
    const secretGroups: SecretGroup[] = [];
    for (const secretGroup of response.secretGroups) {
      const newSecretGroup = new SecretGroup(cloudlyClientRef);
      Object.assign(newSecretGroup, secretGroup);
      secretGroups.push(newSecretGroup);
    }
    return secretGroups;
  }

  public static async createSecretGroup(cloudlyClientRef: CloudlyApiClient, secretGroupDataArg: Partial<plugins.servezoneInterfaces.data.ISecretGroup['data']>) {
    const createSecretGroupTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.secretgroup.IReq_CreateSecretGroup>(
      'createSecretGroup'
    );
    const response = await createSecretGroupTR.fire({
      identity: cloudlyClientRef.identity,
      secretGroup: {
        id: null,
        data: {
          name: secretGroupDataArg.name,
          description: secretGroupDataArg.description,
          environments: secretGroupDataArg.environments,
          key: secretGroupDataArg.key,
          tags: secretGroupDataArg.tags,
          priority: secretGroupDataArg.priority,
        },
      },
    });
    const newSecretGroup = new SecretGroup(cloudlyClientRef);
    Object.assign(newSecretGroup, response.resultSecretGroup);    
    return newSecretGroup;
  }

  // INSTANCE
  public async update() {
    const updateSecretGroupTR = this.cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.secretgroup.IReq_UpdateSecretGroup>(
      'updateSecretGroup'
    );
    const response = await updateSecretGroupTR.fire({
      identity: this.cloudlyClientRef.identity,
      secretGroup: {
        id: this.id,
        data: this.data,
      },
    });

    const resultSecretGroupData = response.resultSecretGroup.data;
    plugins.smartexpect.expect(resultSecretGroupData).toEqual(this.data);

    return this;
  }

  public async delete(cloudlyClientRef: CloudlyApiClient, secretGroupIdArg: string) {
    const deleteSecretGroupTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.secretgroup.IReq_DeleteSecretGroupById>(
      'deleteSecretGroupById'
    );
    const response = await deleteSecretGroupTR.fire({
      identity: cloudlyClientRef.identity,
      secretGroupId: this.id,
    });
    plugins.smartexpect.expect(response.ok).toBeTrue();
    return null;
  }
}
