// https://github.com/airbnb/javascript/#naming--uppercase
import { DEFAULT_INPUT_SHADOW } from "@formbricks/types/dark-palette";
import { TWorkspaceStyling } from "@formbricks/types/workspace";
import { DEFAULT_BRAND_COLOR } from "@/lib/brand-color";
import { ensureReadable, getReadableTextColor, mixColor } from "@/lib/utils/colors";

export const COLOR_DEFAULTS = {
  brandColor: "#1e40af",
  elementHeadlineColor: "#142a72",
  inputBgColor: "#edf0f9",
  inputBorderColor: "#a5b3df",
  cardBackgroundColor: "#f8f9fd",
  cardBorderColor: "#d2d9ef",
  highlightBorderColor: "#1e40af",
} as const;

/**
 * Derives a complete set of suggested color values from a single brand color.
 *
 * Used by the workspace-level "Suggest Colors" button **and** to build
 * `STYLE_DEFAULTS` so that a fresh install always has colours that are
 * visually cohesive with the default brand.
 *
 * The returned object is a flat map of form-field paths to values so it
 * can be spread directly into form defaults or applied via `form.setValue`.
 */
export const getSuggestedColors = (brandColor: string = DEFAULT_BRAND_COLOR) => {
  // Input / option background: white with noticeable brand tint
  const inputBg = mixColor(brandColor, "#ffffff", 0.92);
  // Input border: visible brand-tinted border
  const inputBorder = mixColor(brandColor, "#ffffff", 0.6);
  // Card tones — the card stays white so all derived text renders on a known light surface.
  const cardBg = "#ffffff";
  const cardBorder = mixColor(brandColor, "#ffffff", 0.8);
  // Page background
  const pageBg = mixColor(brandColor, "#ffffff", 0.855);

  // Selected/hovered options darken the option background by 5% (see survey-ui tailwind
  // config: `color-mix(--fb-option-bg-color 95%, black)`), so this is the darkest surface the
  // shared text color renders on — the worst case for contrast.
  const optionSelectedBg = mixColor(inputBg, "#000000", 0.05);

  // Question / dark text: brand darkened with black for a visible tint, then guaranteed to
  // clear WCAG AA against the darkest surface it lands on (the selected option bg, which is
  // slightly darker than the white card and the near-white input bg — so those pass too).
  const questionColor = ensureReadable(mixColor(brandColor, "#000000", 0.35), optionSelectedBg);

  return {
    // General
    "brandColor.light": brandColor,

    // Headlines & Descriptions
    "elementHeadlineColor.light": questionColor,
    "elementDescriptionColor.light": questionColor,
    // Brand-derived like the other text colors so it stays configurable and visually cohesive.
    // The WCAG 1.4.3 fix was dropping the old 0.6 opacity (see globals.css), not muting the hue.
    "elementUpperLabelColor.light": questionColor,

    // Buttons — brand background with a text color chosen for AA contrast against it.
    "buttonBgColor.light": brandColor,
    "buttonTextColor.light": getReadableTextColor(brandColor),

    // Inputs
    "inputBgColor.light": inputBg,
    "inputBorderColor.light": inputBorder,
    "inputTextColor.light": questionColor,

    // Options (Radio / Checkbox)
    "optionBgColor.light": inputBg,
    "optionLabelColor.light": questionColor,
    "optionBorderColor.light": inputBorder,

    // Card
    "cardBackgroundColor.light": cardBg,
    "cardBorderColor.light": cardBorder,

    // Highlight / Focus
    "highlightBorderColor.light": mixColor(brandColor, "#ffffff", 0.25),

    // Progress Bar — indicator uses the brand color; track is a lighter tint.
    "progressIndicatorBgColor.light": brandColor,
    "progressTrackBgColor.light": mixColor(brandColor, "#ffffff", 0.8),

    // Background
    background: { bg: pageBg, bgType: "color" as const, brightness: 100 },
  };
};

// Pre-compute colors derived from the default brand color.
const _colors = getSuggestedColors(DEFAULT_BRAND_COLOR);

/**
 * Single source of truth for every styling default.
 *
 * Color values are derived from the default brand color (#1e40af) via
 * `getSuggestedColors()`.  Non-color values (dimensions, weights, sizes)
 * are hardcoded here and must be kept in sync with globals.css.
 *
 * Used everywhere: form defaults, preview rendering, email templates,
 * and as the reset target for "Restore defaults".
 */
export const STYLE_DEFAULTS: TWorkspaceStyling = {
  allowStyleOverwrite: true,
  brandColor: { light: _colors["brandColor.light"] },
  inputBorderColor: { light: _colors["inputBorderColor.light"] },
  cardBackgroundColor: { light: _colors["cardBackgroundColor.light"] },
  cardBorderColor: { light: _colors["cardBorderColor.light"] },
  isLogoHidden: false,
  highlightBorderColor: { light: _colors["highlightBorderColor.light"] },
  roundness: 8,
  // Link surveys default to the cardless layout; "cardless" is link-only, so app surveys keep "simple".
  cardArrangement: { linkSurveys: "cardless", appSurveys: "simple" },
  linkSurveyCardWidth: "default",

  // Headlines & Descriptions
  elementHeadlineColor: { light: _colors["elementHeadlineColor.light"] },
  elementHeadlineFontSize: 16,
  elementHeadlineFontWeight: 500,
  elementDescriptionColor: { light: _colors["elementDescriptionColor.light"] },
  elementDescriptionFontSize: 14,
  elementDescriptionFontWeight: 400,
  elementUpperLabelColor: { light: _colors["elementUpperLabelColor.light"] },
  elementUpperLabelFontSize: 12,
  elementUpperLabelFontWeight: 400,

  // Inputs
  inputBgColor: { light: _colors["inputBgColor.light"] },
  inputTextColor: { light: _colors["inputTextColor.light"] },
  inputBorderRadius: 8,
  inputHeight: 20,
  inputFontSize: 14,
  inputPaddingX: 8,
  inputPaddingY: 8,
  inputPlaceholderOpacity: 0.5,
  inputShadow: DEFAULT_INPUT_SHADOW,

  // Buttons
  buttonBgColor: { light: _colors["buttonBgColor.light"] },
  buttonTextColor: { light: _colors["buttonTextColor.light"] },
  buttonBorderRadius: 8,
  buttonHeight: "auto",
  buttonFontSize: 16,
  buttonFontWeight: 500,
  buttonPaddingX: 12,
  buttonPaddingY: 12,

  // Options
  optionBgColor: { light: _colors["optionBgColor.light"] },
  optionLabelColor: { light: _colors["optionLabelColor.light"] },
  optionBorderColor: { light: _colors["optionBorderColor.light"] },
  optionBorderRadius: 8,
  optionPaddingX: 16,
  optionPaddingY: 16,
  optionFontSize: 14,

  // Progress Bar
  progressTrackHeight: 8,
  progressTrackBgColor: { light: _colors["progressTrackBgColor.light"] },
  progressIndicatorBgColor: { light: _colors["progressIndicatorBgColor.light"] },
};

/**
 * Builds a complete TWorkspaceStyling object from a single brand color.
 *
 * Uses STYLE_DEFAULTS for all non-color properties (dimensions, weights, etc.)
 * and derives every color from the given brand color via getSuggestedColors().
 *
 * Useful when only a brand color is known (e.g. onboarding) and a fully
 * coherent styling object is needed for both preview rendering and persistence.
 */
export const buildStylingFromBrandColor = (brandColor: string = DEFAULT_BRAND_COLOR): TWorkspaceStyling => {
  const colors = getSuggestedColors(brandColor);

  return {
    ...STYLE_DEFAULTS,
    brandColor: { light: colors["brandColor.light"] },
    elementHeadlineColor: { light: colors["elementHeadlineColor.light"] },
    elementDescriptionColor: { light: colors["elementDescriptionColor.light"] },
    elementUpperLabelColor: { light: colors["elementUpperLabelColor.light"] },
    buttonBgColor: { light: colors["buttonBgColor.light"] },
    buttonTextColor: { light: colors["buttonTextColor.light"] },
    inputBgColor: { light: colors["inputBgColor.light"] },
    inputBorderColor: { light: colors["inputBorderColor.light"] },
    inputTextColor: { light: colors["inputTextColor.light"] },
    optionBgColor: { light: colors["optionBgColor.light"] },
    optionLabelColor: { light: colors["optionLabelColor.light"] },
    optionBorderColor: { light: colors["optionBorderColor.light"] },
    cardBackgroundColor: { light: colors["cardBackgroundColor.light"] },
    cardBorderColor: { light: colors["cardBorderColor.light"] },
    highlightBorderColor: { light: colors["highlightBorderColor.light"] },
    progressIndicatorBgColor: { light: colors["progressIndicatorBgColor.light"] },
    progressTrackBgColor: { light: colors["progressTrackBgColor.light"] },
    background: colors.background,
  };
};
