import { Injectable } from '@angular/core';
import { ConfigService } from '@shared/config/config.service';
import { FleioApiService } from '../../fleio-api.service';
import { HttpClient } from '@angular/common/http';
import { IProductModel } from '../model/product.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs';
import { IConfigOptionModel } from '@fleio-api/billing/model/config-option.model';

@Injectable({
  providedIn: 'root'
})
export class ProductsApiService extends FleioApiService<IProductModel> {
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('billing/products'));
  }

  getConfigurableOptions(productId: FleioId, search=null, excludedIds=null, cycle=null, currency=null): Observable<{
    available_options: Array<Record<string, any>>;
    configurable_options: Array<IConfigOptionModel>;
    services_count: number;
  }> {
    const queryParams = {} as any;
    if (search) {
      queryParams.search_value = search;
    }
    if (excludedIds) {
      queryParams.excluded_ids = excludedIds;
    }
    if (cycle && currency) {
      queryParams.cycle = cycle;
      queryParams.currency = currency;
    }
    return this.objectGetAction(productId, 'configurable_options', queryParams);
  }

  getUpgradeOptions(productId: FleioId): Observable<{
    available_products: Array<IProductModel>;
    selected_products: Array<IProductModel>;
  }> {
    return this.objectGetAction(productId, 'upgrade_options');
  }

  associateOption(productId: FleioId, optionId: FleioId) {
    return this.objectPostAction(productId, 'associate_configurable_option', {
      configurable_option: optionId
    });
  }

  saveUpgrades(productId: FleioId, upgrades: Array<any>) {
    return this.objectPostAction(productId, 'upgrade_options', {upgrades});
  }

  getLinks(productId: FleioId): Observable<Array<{url: string; description: string}>> {
    return this.objectGetAction(productId, 'product_links');
  }
}
