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

export const servicesService = {
  // ============================================================================
  // Public (non-personalized) catalog endpoints
  // ============================================================================

  async getPublicInternetCatalog(): Promise<InternetCatalogCollection> {
    const response = await apiClient.GET<InternetCatalogCollection>(
      "/api/public/services/internet/plans"
    );
    return getDataOrThrow(response, "Failed to load internet services");
  },

  async getPublicSimCatalog(): Promise<SimCatalogCollection> {
    const response = await apiClient.GET<SimCatalogCollection>("/api/public/services/sim/plans");
    return getDataOrDefault(response, EMPTY_SIM_CATALOG);
  },

  async getPublicVpnCatalog(): Promise<VpnCatalogCollection> {
    const response = await apiClient.GET<VpnCatalogCollection>("/api/public/services/vpn/plans");
    return getDataOrDefault(response, EMPTY_VPN_CATALOG);
  },

  // ============================================================================
  // Account (authenticated + personalized) catalog endpoints
  // ============================================================================

  async getAccountInternetCatalog(): Promise<InternetCatalogCollection> {
    const response = await apiClient.GET<InternetCatalogCollection>(
      "/api/account/services/internet/plans"
    );
    return getDataOrThrow(response, "Failed to load internet services");
  },

  async getAccountSimCatalog(): Promise<SimCatalogCollection> {
    const response = await apiClient.GET<SimCatalogCollection>("/api/account/services/sim/plans");
    return getDataOrDefault(response, EMPTY_SIM_CATALOG);
  },

  async getAccountVpnCatalog(): Promise<VpnCatalogCollection> {
    const response = await apiClient.GET<VpnCatalogCollection>("/api/account/services/vpn/plans");
    return getDataOrDefault(response, EMPTY_VPN_CATALOG);
  },

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

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

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

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

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

  async getInternetEligibility(): Promise<InternetEligibilityDetails> {
    const response = await apiClient.GET<InternetEligibilityDetails>(
      "/api/services/internet/eligibility"
    );
    const data = getDataOrThrow(response, "Failed to load internet eligibility");
    return internetEligibilityDetailsSchema.parse(data);
  },

  async requestInternetEligibilityCheck(body?: {
    notes?: string;
    address?: Partial<Address>;
  }): Promise<{ requestId: string }> {
    const response = await apiClient.POST<{ requestId: string }>(
      "/api/services/internet/eligibility-request",
      {
        body: body ?? {},
      }
    );
    return getDataOrThrow(response, "Failed to request availability check");
  },
};
