import Failure from "core/failure";
import { Service } from "services/domain/serviceEntity";
import { SubscriptionsStore } from "subscriptions/domain/subscriptionsStore";

type SubscribeServiceStore = Pick<
  SubscriptionsStore,
  "subscriptions" | "subscribe"
>;

const serviceSubscribeUseCase = async (
  store: SubscribeServiceStore,
  id: Service["id"],
  password: string
): Promise<void> => {
  if (store.subscriptions?.includes(id)) {
    return Promise.reject(
      new Failure({
        key: "failures.subscription.exists",
        message: "Service subscription already eixsts",
      })
    );
  }
  return store.subscribe({
    password: password,
    service_id: id,
  });
};

export { serviceSubscribeUseCase };
