import { useSegments } from "expo-router";
import { useMedia } from "../context/MediaContext";

/** GlobalMiniPlayer's own height. */
export const MINI_PLAYER_HEIGHT = 52;
/** Breathing room between the mini-player and whatever content sits just above it. */
const MINI_PLAYER_CLEARANCE = 16;
/** Total bottom space a screen should reserve so its content doesn't render underneath the mini-player. */
export const MINI_PLAYER_SPACE = MINI_PLAYER_HEIGHT + MINI_PLAYER_CLEARANCE;

/**
 * Whether GlobalMiniPlayer is showing on the *current* screen — the single
 * source of truth both GlobalMiniPlayer itself and every page's
 * bottom-padding logic key off of, so the two can't silently drift apart
 * (e.g. a page reserving space the bar isn't actually using, or vice versa).
 */
export function useMiniPlayerVisible(): boolean {
  const { activeTrack } = useMedia();
  const segments = useSegments();
  if (!activeTrack) return false;
  // Hidden on the login screen (nothing meaningful to show pre-auth) and on
  // the Player tab itself, which already shows a full transport bar.
  const isLogin = segments[0] === "login";
  const isPlayerTab = segments[0] === "(tabs)" && segments[1] === "player";
  return !isLogin && !isPlayerTab;
}

/**
 * Extra bottom space (points) a screen should add — e.g. a list's
 * `contentContainerStyle` paddingBottom, or a floating action button's
 * `bottom` offset — so its content clears the global mini-player instead of
 * rendering underneath it. 0 when the mini-player isn't showing here.
 */
export function useMiniPlayerInset(): number {
  return useMiniPlayerVisible() ? MINI_PLAYER_SPACE : 0;
}
