"use client";

import {
  DEFAULT_CHARACTER_ID,
  DEFAULT_CHARACTER_SLUG,
  getCharacterBySlug,
} from "@/data/constants/character";
import { buildGlobalPageUrl } from "@/router/global-route-context";
import { getCharacterRoutes, ROUTES } from "@/router/routes";
import type { AppSubscriptionReturnTo } from "@/router/navigation-types";

import { consumePendingChatImageReturn } from "./chat_image_return_session";
import {
  clearPendingChatUnlock,
  peekPendingChatUnlock,
} from "./chat_unlock_session";

export type SubscriptionReturnTo = AppSubscriptionReturnTo;

export async function consumeSubscriptionExplicitExitUrl(
  characterSlug: string = DEFAULT_CHARACTER_SLUG,
): Promise<string | null> {
  const characterId = resolveCharacterId(characterSlug);
  const pendingImageReturn = await consumePendingChatImageReturn(characterId);
  if (pendingImageReturn) return pendingImageReturn.returnUrl;
  const pendingChatUnlock = await peekPendingChatUnlock(characterId);
  if (pendingChatUnlock) {
    await clearPendingChatUnlock();
    return pendingChatUnlock.returnUrl;
  }
  return null;
}

export async function peekSubscriptionExplicitExitUrl(
  characterSlug: string = DEFAULT_CHARACTER_SLUG,
): Promise<string | null> {
  const pendingChatUnlock = await peekPendingChatUnlock(
    resolveCharacterId(characterSlug),
  );
  if (pendingChatUnlock) return pendingChatUnlock.returnUrl;
  return null;
}

export function getSubscriptionFallbackExitUrl(
  returnTo: SubscriptionReturnTo,
  characterSlug: string = DEFAULT_CHARACTER_SLUG,
): string {
  const routes = getCharacterRoutes(characterSlug);
  if (returnTo === "chat") return routes.chat;
  if (returnTo === "private-zone") return routes.privateZone;
  if (returnTo === "profile") {
    return buildGlobalPageUrl(ROUTES.profile, routes.chat);
  }
  return ROUTES.profile;
}

export async function consumeSubscriptionExitUrl(
  returnTo: SubscriptionReturnTo,
  characterSlug: string = DEFAULT_CHARACTER_SLUG,
): Promise<string> {
  if (returnTo === "private-zone") {
    return getCharacterRoutes(characterSlug).privateZone;
  }
  if (returnTo === "profile") {
    return getSubscriptionFallbackExitUrl(returnTo, characterSlug);
  }

  return (
    (await consumeSubscriptionExplicitExitUrl(characterSlug)) ??
    getSubscriptionFallbackExitUrl(returnTo, characterSlug)
  );
}

export async function resolveSubscriptionSuccessExitUrl(
  returnTo: SubscriptionReturnTo,
  characterSlug: string = DEFAULT_CHARACTER_SLUG,
): Promise<string> {
  if (returnTo === "private-zone") {
    return getCharacterRoutes(characterSlug).privateZone;
  }
  if (returnTo === "profile") {
    return getSubscriptionFallbackExitUrl(returnTo, characterSlug);
  }

  const pendingExitUrl = await peekSubscriptionExplicitExitUrl(characterSlug);
  if (pendingExitUrl) return pendingExitUrl;
  return consumeSubscriptionExitUrl(returnTo, characterSlug);
}

function resolveCharacterId(characterSlug: string): string {
  return getCharacterBySlug(characterSlug)?.id ?? DEFAULT_CHARACTER_ID;
}
