/**
 * Shared theme constants for PixieSprout.
 * Colors, haptic feedback, and the pressWithHaptic helper.
 */
import * as Haptics from 'expo-haptics';

// ---- Brand Colors ----
const DEEP_LEAF = '#2E6B3F';

export const brand = {
  name: 'PixieSprout',
  tagline: 'Plant care, with a little magic.',
  colors: {
    deepLeaf: DEEP_LEAF,
    freshSprout: '#78C66A',
    softSage: '#BFD9B4',
    warmCream: '#F8F4E8',
    petalBlush: '#F4C6B8',
  },
};

// 'green' = the primary action/button color. 2026-08-06: unified to the rich
// forest green (#2E6B3F, same as deepLeaf/dark) so every green button, accent,
// and active state matches the "Got it" guidance button Bekky loved. Darker than
// the old freshSprout (#78C66A), so white-on-green button text gains contrast.
export const green = DEEP_LEAF;
export const lime = '#8DC63F';
export const dark = brand.colors.deepLeaf;
export const parchment = '#FDF4DB';
export const line = brand.colors.softSage;
export const brown = '#8A783D';

// ---- Haptic Feedback ----
type HapticFeedback = () => Promise<void>;

function runHapticSafely(feedback: HapticFeedback): Promise<void | undefined> {
  try {
    return Promise.resolve(feedback()).catch(() => undefined);
  } catch {
    return Promise.resolve(undefined);
  }
}

export const haptic = {
  light: (): Promise<void | undefined> =>
    runHapticSafely(() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)),
  scan: (): Promise<void | undefined> =>
    runHapticSafely(() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium)),
  success: (): Promise<void | undefined> =>
    runHapticSafely(() => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success)),
  warning: (): Promise<void | undefined> =>
    runHapticSafely(() => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning)),
};

export function pressWithHaptic<T extends unknown[]>(
  handler?: (...args: T) => unknown,
  feedback: () => Promise<void> = haptic.light,
): (...args: T) => void {
  return (...args: T): void => {
    // Haptic failures must never block the actual press handler or create an
    // unhandled promise rejection (for example on unsupported platforms).
    void runHapticSafely(feedback);

    if (typeof handler === 'function') {
      handler(...args);
    }
  };
}
