import { apiRequest } from "@/lib/api/api-service";
import type { ApiResponse } from "@/types/api-response";
import type {
  PlanInfo,
  SubscriptionDto,
  CheckoutConfigDto,
  CreateCheckoutRequest,
  CancelSubscriptionRequest,
} from "./types";

/**
 * Subscriptions Service
 * Backend: /api/subscriptions/*
 */

const getPlans = (): Promise<ApiResponse<readonly PlanInfo[]>> => {
  return apiRequest<ApiResponse<readonly PlanInfo[]>>({
    url: "/subscriptions/plans",
    client: "core",
    method: "get",
  });
};

const getMySubscription = (): Promise<ApiResponse<SubscriptionDto | null>> => {
  return apiRequest<ApiResponse<SubscriptionDto | null>>({
    url: "/subscriptions/me",
    client: "core",
    method: "get",
  });
};

const getCheckoutConfig = (
  dto: CreateCheckoutRequest,
): Promise<ApiResponse<CheckoutConfigDto>> => {
  return apiRequest<ApiResponse<CheckoutConfigDto>>({
    url: "/subscriptions/checkout",
    client: "core",
    method: "post",
    data: dto,
  });
};

const cancelSubscription = (
  dto: CancelSubscriptionRequest,
): Promise<ApiResponse<null>> => {
  return apiRequest<ApiResponse<null>>({
    url: "/subscriptions/cancel",
    client: "core",
    method: "post",
    data: dto,
  });
};

export const subscriptionService = {
  getPlans,
  getMySubscription,
  getCheckoutConfig,
  cancelSubscription,
};
