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

export class Service implements plugins.servezoneInterfaces.data.IService {
  public static async getServices(cloudlyClientRef: CloudlyApiClient) {
    const getAllServicesTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.service.IRequest_Any_Cloudly_GetServices>(
      'getServices'
    );
    const response = await getAllServicesTR.fire({
      identity: cloudlyClientRef.identity,
    });
    const resultServices: Service[] = [];
    for (const service of response.services) {
      const newService = new Service(cloudlyClientRef);
      Object.assign(newService, service);
      resultServices.push(newService);
    }
    return resultServices;
  }

  public static async getServiceById(cloudlyClientRef: CloudlyApiClient, serviceIdArg: string) {
    const getServiceByIdTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.service.IRequest_Any_Cloudly_GetServiceById>(
      'getServiceById'
    );
    const response = await getServiceByIdTR.fire({
      identity: cloudlyClientRef.identity,
      serviceId: serviceIdArg,
    });
    const newService = new Service(cloudlyClientRef);
    Object.assign(newService, response.service);
    return newService;
  }

  /**
   * creates a new service 
   */
  public static async createService(cloudlyClientRef: CloudlyApiClient, serviceDataArg: Partial<plugins.servezoneInterfaces.data.IService['data']>) {
    const createServiceTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.service.IRequest_Any_Cloudly_CreateService>(
      'createService'
    );
    const response = await createServiceTR.fire({
      identity: cloudlyClientRef.identity,
      serviceData: serviceDataArg as plugins.servezoneInterfaces.data.IService['data'],
    });
    const newService = new Service(cloudlyClientRef);
    Object.assign(newService, response.service);
    return newService;
  }

  // INSTANCE
  cloudlyClientRef: CloudlyApiClient;

  public id: string;
  public data: plugins.servezoneInterfaces.data.IService['data'];

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

  /**
   * The service has a secret bundle.
   * This function essentially returns the secret bundle as a flat object.
   * In other words, it resolves secret groups and 
   */
  public async getSecretBundleAsFlatObject(environmentArg: string = 'production') {
    const getServiceSecretBundlesAsFlatObjectTR = this.cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.service.IRequest_Any_Cloudly_GetServiceSecretBundlesAsFlatObject>(
      'getServiceSecretBundlesAsFlatObject'
    );
    const response = await getServiceSecretBundlesAsFlatObjectTR.fire({
      identity: this.cloudlyClientRef.identity,
      serviceId: this.id,
      environment: environmentArg,
    });
    const flatKeyValueObject: {[key: string]: string} = response.flatKeyValueObject;

    return flatKeyValueObject;
  }
}