import type { PrivateAlbumUnlockResponse } from "@/data/schemas/private-zone";

import type { PrivateZoneUnlockPaywallRequest } from "../private-zone-state";

export function toPrivateZoneErrorMessage(error: unknown): string {
  if (error instanceof Error && error.message) return error.message;
  return "Private Zone is temporarily unavailable. Please try again.";
}

export function toUnlockErrorMessage(
  response: PrivateAlbumUnlockResponse,
): string | null {
  switch (response.reason) {
    case "cost_changed":
      return "Unlock price changed. Please confirm again.";
    case "unlock_in_progress":
      return "This album is already unlocking. Please try again shortly.";
    case "deduct_failed":
      return "Credit deduction failed. Please try again.";
    case "persist_failed_refunded":
      return "Unlock failed and your credits were refunded. Please try again.";
    case "not_found":
      return "This private album is no longer available.";
    default:
      return response.unlocked && !response.locked
        ? null
        : "Unlock failed. Please try again.";
  }
}

export function toPaywallRequest(
  response: PrivateAlbumUnlockResponse,
): PrivateZoneUnlockPaywallRequest {
  return {
    albumId: response.albumId,
    reason: response.reason,
    requiredCredits: response.requiredCredits || response.unlockCost,
    currentCredits: response.creditBalance,
    shortfallCredits: response.shortfallCredits,
    ...(response.paymentGuidance
      ? { paymentGuidance: response.paymentGuidance }
      : {}),
  };
}
