import { apiClient, getDataOrThrow } from "@/core/api";
import type {
  InternetCancelRequest,
  InternetCancellationPreview,
} from "@customer-portal/domain/subscriptions";
import { internetCancellationPreviewSchema } from "@customer-portal/domain/subscriptions";

export const internetActionsService = {
  /**
   * Get cancellation preview (available months, service details)
   */
  async getCancellationPreview(subscriptionId: string): Promise<InternetCancellationPreview> {
    const response = await apiClient.GET<InternetCancellationPreview>(
      "/api/subscriptions/{subscriptionId}/internet/cancellation-preview",
      {
        params: { path: { subscriptionId } },
      }
    );

    const payload = getDataOrThrow(response, "Failed to load cancellation information");
    return internetCancellationPreviewSchema.parse(payload);
  },

  /**
   * Submit Internet cancellation request
   */
  async submitCancellation(subscriptionId: string, request: InternetCancelRequest): Promise<void> {
    await apiClient.POST("/api/subscriptions/{subscriptionId}/internet/cancel", {
      params: { path: { subscriptionId } },
      body: request,
    });
  },
};
