import { apiClient, getDataOrDefault, getDataOrThrow } from "@/lib/api";
import {
  EMPTY_SIM_CATALOG,
  EMPTY_VPN_CATALOG,
  internetInstallationCatalogItemSchema,
  internetAddonCatalogItemSchema,
  simActivationFeeCatalogItemSchema,
  simCatalogProductSchema,
  vpnCatalogProductSchema,
  type InternetCatalogCollection,
  type InternetAddonCatalogItem,
  type InternetInstallationCatalogItem,
  type SimActivationFeeCatalogItem,
  type SimCatalogCollection,
  type SimCatalogProduct,
  type VpnCatalogCollection,
  type VpnCatalogProduct,
} from "@customer-portal/domain/catalog";

export const catalogService = {
  async getInternetCatalog(): Promise<InternetCatalogCollection> {
    const response = await apiClient.GET<InternetCatalogCollection>("/api/catalog/internet/plans");
    const data = getDataOrThrow<InternetCatalogCollection>(
      response,
      "Failed to load internet catalog"
    );
    return data; // BFF already validated
  },

  async getInternetInstallations(): Promise<InternetInstallationCatalogItem[]> {
    const response = await apiClient.GET<InternetInstallationCatalogItem[]>(
      "/api/catalog/internet/installations"
    );
    const data = getDataOrDefault<InternetInstallationCatalogItem[]>(response, []);
    return internetInstallationCatalogItemSchema.array().parse(data);
  },

  async getInternetAddons(): Promise<InternetAddonCatalogItem[]> {
    const response = await apiClient.GET<InternetAddonCatalogItem[]>(
      "/api/catalog/internet/addons"
    );
    const data = getDataOrDefault<InternetAddonCatalogItem[]>(response, []);
    return internetAddonCatalogItemSchema.array().parse(data);
  },

  async getSimCatalog(): Promise<SimCatalogCollection> {
    const response = await apiClient.GET<SimCatalogCollection>("/api/catalog/sim/plans");
    const data = getDataOrDefault<SimCatalogCollection>(response, EMPTY_SIM_CATALOG);
    return data; // BFF already validated
  },

  async getSimActivationFees(): Promise<SimActivationFeeCatalogItem[]> {
    const response = await apiClient.GET<SimActivationFeeCatalogItem[]>(
      "/api/catalog/sim/activation-fees"
    );
    const data = getDataOrDefault<SimActivationFeeCatalogItem[]>(response, []);
    return simActivationFeeCatalogItemSchema.array().parse(data);
  },

  async getSimAddons(): Promise<SimCatalogProduct[]> {
    const response = await apiClient.GET<SimCatalogProduct[]>("/api/catalog/sim/addons");
    const data = getDataOrDefault<SimCatalogProduct[]>(response, []);
    return simCatalogProductSchema.array().parse(data);
  },

  async getVpnCatalog(): Promise<VpnCatalogCollection> {
    const response = await apiClient.GET<VpnCatalogCollection>("/api/catalog/vpn/plans");
    const data = getDataOrDefault<VpnCatalogCollection>(response, EMPTY_VPN_CATALOG);
    return data; // BFF already validated
  },

  async getVpnActivationFees(): Promise<VpnCatalogProduct[]> {
    const response = await apiClient.GET<VpnCatalogProduct[]>("/api/catalog/vpn/activation-fees");
    const data = getDataOrDefault<VpnCatalogProduct[]>(response, []);
    return vpnCatalogProductSchema.array().parse(data);
  },
};
