import type { CSSProperties } from 'react';
import type { TreatmentCatalogEntry } from '@/types/treatment-catalog';
import {
  resolveCatalogTypeColor,
  TREATMENT_TYPE_COLORS,
} from '@/components/shared/catalog-type-colors';

/**
 * Single source of truth for treatment-type colors across the app
 * (treatment detail dropdown, appointment booking dropdown, appointment legend,
 * appointment schedule banners, and the treatment feature's appointment cards).
 *
 * There is no universal dental type→color standard (only status-based blue/red
 * conventions), so this is a curated pastel palette. Extend it as new treatment
 * types are added; unknown codes fall back to a rotating pastel set by index.
 */

/** Dark ink that stays readable on every pastel in the palette. */
const BANNER_INK = '#14253d';
/** Dark background used behind pastel option text in native <select> dropdowns. */
export const DROPDOWN_OPTION_BG = '#14253d';

export function treatmentTypeColor(code: string, index = 0): string {
  return resolveCatalogTypeColor(code, TREATMENT_TYPE_COLORS, index);
}

/** Filled swatch (legend dots, small indicators). */
export function treatmentTypeSwatchStyle(code: string, index = 0): CSSProperties {
  const color = treatmentTypeColor(code, index);
  return { backgroundColor: color, borderColor: 'rgba(0, 0, 0, 0.18)' };
}

/** Colored banner / card fill with readable dark text (schedule blocks, appointment cards). */
export function treatmentTypeBannerStyle(code: string, index = 0): CSSProperties {
  const color = treatmentTypeColor(code, index);
  return {
    backgroundColor: color,
    borderColor: 'rgba(0, 0, 0, 0.16)',
    color: BANNER_INK,
  };
}

/** Pastel option text on the dark dropdown background. */
export function treatmentTypeOptionStyle(code: string, index = 0): CSSProperties {
  return { color: treatmentTypeColor(code, index), backgroundColor: DROPDOWN_OPTION_BG };
}

export function treatmentTypeLabelFromCatalog(
  code: string,
  catalog: TreatmentCatalogEntry[],
): string {
  return catalog.find((e) => e.code === code)?.label ?? formatCodeAsLabel(code);
}

export function formatCodeAsLabel(code: string): string {
  return code
    .split('_')
    .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
    .join(' ');
}

/** @deprecated Use treatmentTypeLabelFromCatalog with API catalog */
export function treatmentTypeLabelKey(code: string): string {
  return `type_${code}`;
}
