import { Injectable, Inject } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Logger } from "nestjs-pino";
import { SalesforceConnection } from "@bff/integrations/salesforce/services/salesforce-connection.service";
import { BaseCatalogService } from "./base-catalog.service";
import type {
  SalesforceProduct2WithPricebookEntries,
  VpnCatalogProduct,
} from "@customer-portal/domain/catalog";
import { Providers as CatalogProviders } from "@customer-portal/domain/catalog";

@Injectable()
export class VpnCatalogService extends BaseCatalogService {
  constructor(
    sf: SalesforceConnection,
    configService: ConfigService,
    @Inject(Logger) logger: Logger
  ) {
    super(sf, configService, logger);
  }
  async getPlans(): Promise<VpnCatalogProduct[]> {
    const soql = this.buildCatalogServiceQuery("VPN", ["VPN_Region__c", "Catalog_Order__c"]);
    const records = await this.executeQuery<SalesforceProduct2WithPricebookEntries>(
      soql,
      "VPN Plans"
    );

    return records.map(record => {
      const entry = this.extractPricebookEntry(record);
      const product = CatalogProviders.Salesforce.mapVpnProduct(record, entry);
      return {
        ...product,
        description: product.description || product.name,
      } satisfies VpnCatalogProduct;
    });
  }

  async getActivationFees(): Promise<VpnCatalogProduct[]> {
    const soql = this.buildProductQuery("VPN", "Activation", ["VPN_Region__c"]);
    const records = await this.executeQuery<SalesforceProduct2WithPricebookEntries>(
      soql,
      "VPN Activation Fees"
    );

    return records.map(record => {
      const pricebookEntry = this.extractPricebookEntry(record);
      const product = CatalogProviders.Salesforce.mapVpnProduct(record, pricebookEntry);

      return {
        ...product,
        description: product.description ?? product.name,
      } satisfies VpnCatalogProduct;
    });
  }

  async getCatalogData() {
    const [plans, activationFees] = await Promise.all([this.getPlans(), this.getActivationFees()]);
    return { plans, activationFees };
  }
}
