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.js";
import { BaseServicesService } from "./base-services.service.js";
import { ServicesCacheService } from "./services-cache.service.js";
import type { VpnCatalogProduct } from "@customer-portal/domain/services";
import type { SalesforceProduct2WithPricebookEntries } from "@customer-portal/domain/services/providers";
import { mapVpnProduct } from "@customer-portal/domain/services/providers";

@Injectable()
export class VpnServicesService extends BaseServicesService {
  constructor(
    sf: SalesforceConnection,
    configService: ConfigService,
    @Inject(Logger) logger: Logger,
    private readonly catalogCache: ServicesCacheService
  ) {
    super(sf, configService, logger);
  }
  async getPlans(): Promise<VpnCatalogProduct[]> {
    const cacheKey = this.catalogCache.buildServicesKey("vpn", "plans");

    return this.catalogCache.getCachedServices(
      cacheKey,
      async () => {
        const soql = this.buildServicesQuery("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 = mapVpnProduct(record, entry);
          return {
            ...product,
            description: product.description || product.name,
          } satisfies VpnCatalogProduct;
        });
      },
      {
        resolveDependencies: plans => ({
          productIds: plans.map(plan => plan.id).filter((id): id is string => Boolean(id)),
        }),
      }
    );
  }

  async getActivationFees(): Promise<VpnCatalogProduct[]> {
    const cacheKey = this.catalogCache.buildServicesKey("vpn", "activation-fees");

    return this.catalogCache.getCachedServices(
      cacheKey,
      async () => {
        const soql = this.buildProductQuery("VPN", "Activation", [
          "VPN_Region__c",
          "Catalog_Order__c",
        ]);
        const records = await this.executeQuery<SalesforceProduct2WithPricebookEntries>(
          soql,
          "VPN Activation Fees"
        );

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

          return {
            ...product,
            description: product.description ?? product.name,
          } satisfies VpnCatalogProduct;
        });
      },
      {
        resolveDependencies: fees => ({
          productIds: fees.map(fee => fee.id).filter((id): id is string => Boolean(id)),
        }),
      }
    );
  }

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