/**
 * Shared badge config: value → { label, class } for reuse across tables.
 * Use in B2CDataTable columns (type: 'badge', valueMap) or in BaseStatusBadge.
 * Same Tailwind classes = consistent look (activity log, status columns, etc.).
 */

import { PROJECT_PHASE_KEYS, PROJECT_PHASE_LABELS } from "./projectPhases.js";

/** Base classes applied to every status badge (shape + size). */
export const BADGE_BASE_CLASSES =
  "inline-flex px-2.5 p-0.5 rounded text-normal font-normal";

/** Default entry when value is not in the map (e.g. unknown event). */
export const DEFAULT_BADGE_ENTRY = {
  label: "—",
  class:
    "bg-surface-200 text-surface-700 dark:bg-surface-600 dark:text-surface-200",
};

/**
 * Activity log event → { label, class }. Used when showing raw event (e.g. filter).
 * For the Actie column we use description + ACTIVITY_CATEGORY_BADGES instead.
 */
export const EVENT_BADGES = {
  created: {
    label: "Created",
    class:
      "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300",
  },
  updated: {
    label: "Updated",
    class: "bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300",
  },
  deleted: {
    label: "Deleted",
    class: "bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300",
  },
  restored: {
    label: "Restored",
    class:
      "bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-300",
  },
  login: {
    label: "Login",
    class: "bg-teal-100 text-teal-800 dark:bg-teal-900/30 dark:text-teal-300",
  },
  logout: {
    label: "Logout",
    class: "bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300",
  },
  export: {
    label: "Export",
    class:
      "bg-amber-100 text-amber-800 dark:bg-amber-900/30 dark:text-amber-300",
  },
};

/**
 * Activity log event → Dutch label (for Actie column and filter options).
 * Use getEventLabelNl(event) for display; falls back to EVENT_BADGES label when missing.
 */
export const EVENT_LABEL_NL = {
  created: "Aanmaken",
  updated: "Bewerken",
  deleted: "Verwijderen",
  restored: "Herstellen",
  login: "Inloggen",
  logout: "Uitloggen",
  export: "Export",
};

export function getEventLabelNl(event) {
  return EVENT_LABEL_NL[event] ?? EVENT_BADGES[event]?.label ?? "Actie";
}

/**
 * Activity log Actie column: one badge style per action (same keys as EVENT_LABEL_NL).
 * Used as valueMap for the event column; unknown events use default.
 */
export const ACTIVITY_CATEGORY_BADGES = {
  created: {
    class: EVENT_BADGES.created.class,
  },
  updated: {
    class: EVENT_BADGES.updated.class,
  },
  deleted: {
    class: EVENT_BADGES.deleted.class,
  },
  restored: {
    class: EVENT_BADGES.restored.class,
  },
  login: {
    class: EVENT_BADGES.login.class,
  },
  logout: {
    class: EVENT_BADGES.logout.class,
  },
  export: {
    class: EVENT_BADGES.export.class,
  },
  default: {
    class: DEFAULT_BADGE_ENTRY.class,
  },
};

/**
 * Preset color classes for building your own valueMap (same visual style as EVENT_BADGES).
 * Example: { label: 'Actief', class: PRESET_BADGE_CLASSES.success }
 */
export const PRESET_BADGE_CLASSES = {
  success:
    "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300",
  info: "bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300",
  danger: "bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300",
  warn: "bg-amber-100 text-amber-800 dark:bg-amber-900/30 dark:text-amber-300",
  neutral: "bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300",
  purple:
    "bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-300",
  teal: "bg-teal-100 text-teal-800 dark:bg-teal-900/30 dark:text-teal-300",
  secondary:
    "bg-surface-200 text-surface-700 dark:bg-surface-600 dark:text-surface-200",
};

/**
 * Foreground-only classes matching the text tokens on {@link PRESET_BADGE_CLASSES}
 * `success` and `secondary` (no `bg-*`). Does not replace `PRESET_BADGE_CLASSES`.
 * **`Project/Dashboard/Index.vue`** phase row uses the same tokens (`text-primary` / `dark:text-primary-300`); kept here for tables / reuse.
 */
export const PRESET_BADGE_TEXT_ONLY_CLASSES = {
  success: "text-green-800 dark:text-green-300",
  secondary: "text-surface-700 dark:text-surface-200",
};

/**
 * PrimeVue severity name → our badge class. Use in B2CDataTable so badge type always renders as span (one consistent style).
 */
export const SEVERITY_TO_BADGE_CLASS = {
  success: PRESET_BADGE_CLASSES.success,
  info: PRESET_BADGE_CLASSES.info,
  warn: PRESET_BADGE_CLASSES.warn,
  danger: PRESET_BADGE_CLASSES.danger,
  secondary: PRESET_BADGE_CLASSES.secondary,
  contrast: PRESET_BADGE_CLASSES.neutral,
};

/**
 * Actief / Inactief for boolean is_active columns. Keys are string 'true' / 'false' (row value via String()).
 * Use: { key: 'is_active', header: 'Actief', type: 'badge', valueMap: ACTIVE_INACTIVE_BADGES, ... }
 */
export const ACTIVE_INACTIVE_BADGES = {
  true: { label: "Actief", class: PRESET_BADGE_CLASSES.success },
  false: { label: "Inactief", class: PRESET_BADGE_CLASSES.secondary },
};

/** Configured / Niet geconfigured for has_client_secret etc. */
export const CONFIGURED_BADGES = {
  true: { label: "Geconfigureerd", class: PRESET_BADGE_CLASSES.success },
  false: {
    label: "Niet geconfigureerd",
    class: PRESET_BADGE_CLASSES.secondary,
  },
};

/** OK / Missing for permission check (e.g. Graph test connection). */
export const PERMISSION_CHECK_BADGES = {
  true: { label: "OK", class: PRESET_BADGE_CLASSES.success },
  false: { label: "Missing", class: PRESET_BADGE_CLASSES.secondary },
};

/** Valid / Expired for token status. */
export const TOKEN_VALID_BADGES = {
  true: { label: "Valid", class: PRESET_BADGE_CLASSES.success },
  false: { label: "Expired", class: PRESET_BADGE_CLASSES.danger },
};

/** User identity source: local vs Microsoft (Entra) sync. */
export const IDENTITY_SOURCE_BADGES = {
  local: { label: "Lokaal", class: PRESET_BADGE_CLASSES.info },
  microsoft: { label: "Microsoft", class: PRESET_BADGE_CLASSES.teal },
};

/** User type / guest status: Microsoft, Guest (linked), or pending (for users table column). */
export const USER_TYPE_BADGES = {
  microsoft: { label: "Microsoft", class: PRESET_BADGE_CLASSES.teal },
  guest_linked: { label: "Guest", class: PRESET_BADGE_CLASSES.info },
  guest_pending: {
    label: "Nog niet gekoppeld",
    class: PRESET_BADGE_CLASSES.warn,
  },
};

/** Azure connection status (unified token check). */
export const CONNECTION_STATUS_BADGES = {
  connected: {
    label: "Verbonden met Azure",
    class: PRESET_BADGE_CLASSES.success,
  },
  not_connected: {
    label: "Niet verbonden",
    class: PRESET_BADGE_CLASSES.danger,
  },
  not_configured: {
    label: "Niet geconfigureerd",
    class: PRESET_BADGE_CLASSES.warn,
  },
  unknown: {
    label: "Geen rechten om te testen",
    class: PRESET_BADGE_CLASSES.secondary,
  },
  loading: { label: "Controleren...", class: PRESET_BADGE_CLASSES.secondary },
};

/**
 * Ja / Nee badges for boolean columns. Keys are string 'true' / 'false'.
 */
export const YES_NO_BADGES = {
  true: { label: "Ja", class: PRESET_BADGE_CLASSES.success },
  false: { label: "Nee", class: PRESET_BADGE_CLASSES.secondary },
};

/**
 * Timeline visibility badges for default tasks.
 */
export const TIMELINE_VISIBILITY_BADGES = {
  true: {
    label: "Zichtbaar",
    icon: "pi pi-eye",
    class: PRESET_BADGE_CLASSES.info,
  },
  false: {
    label: "Verborgen",
    icon: "pi pi-eye-slash",
    class: PRESET_BADGE_CLASSES.secondary,
  },
};

/**
 * Checklist count badges (settings/checklists).
 */
export const CHECKLIST_ITEMS_COUNT_BADGE_DEFAULT = {
  label: "0",
  class:
    "border border-surface-300 bg-transparent text-surface-700 dark:border-surface-600 dark:bg-transparent dark:text-surface-100 tabular-nums",
};

export const CHECKLIST_REQUIRED_ITEMS_COUNT_BADGE_DEFAULT = {
  label: "0",
  class:
    "border border-red-300 bg-red-50 text-red-700 dark:border-red-700 dark:bg-red-900/30 dark:text-red-300",
};

/**
 * “Verplicht” pill for required tasks and required task checklist items (`Project/Taken.vue`).
 */
export const TASK_REQUIRED_BADGE = {
  label: "Verplicht",
  class:
    "rounded-full border border-red-300 bg-red-50 text-red-700 dark:border-red-700 dark:bg-red-900/30 dark:text-red-300 text-xs leading-none px-2! py-0.5!",
};

/**
 * Task deadline date pill (`Project/Taken.vue`): shared shape + per-state background/text.
 * Use `getTaskDeadlineDateBadgeClasses(isoDate)` for the full class string.
 */
export const TASK_DEADLINE_DATE_BADGE = {
  base: "inline-flex min-w-0 items-center rounded-full px-2 py-0.5 text-xs font-medium",
  tones: {
    empty:
      "bg-surface-200 text-surface-700 dark:bg-surface-700 dark:text-surface-200",
    overdue: "bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300",
    soon: "bg-amber-100 text-amber-800 dark:bg-amber-900/30 dark:text-amber-300",
    ok: "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300",
  },
};

/**
 * Neutral pill for missing optional values (e.g. unassigned, “—” placeholders).
 * Same visual as an empty task deadline badge; reuse anywhere a compact absent-state is needed.
 */
export const ABSENT_PLACEHOLDER_BADGE_CLASS = `${TASK_DEADLINE_DATE_BADGE.base} ${TASK_DEADLINE_DATE_BADGE.tones.empty}`;

function parseDeadlineDateOnly(value) {
  if (!value) return null;
  const parsed = new Date(`${value}T00:00:00`);
  return Number.isNaN(parsed.getTime()) ? null : parsed;
}

/**
 * Full Tailwind class string for a task deadline date badge (base + tone).
 * Empty / invalid dates use {@link ABSENT_PLACEHOLDER_BADGE_CLASS}.
 * @param {string|null|undefined} value - ISO date (Y-m-d)
 */
export function getTaskDeadlineDateBadgeClasses(value) {
  const deadline = parseDeadlineDateOnly(value);
  if (!deadline) {
    return ABSENT_PLACEHOLDER_BADGE_CLASS;
  }
  const now = new Date();
  const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  const diffDays = Math.floor(
    (deadline.getTime() - today.getTime()) / 86400000,
  );
  let toneKey;
  if (diffDays < 0) toneKey = "overdue";
  else if (diffDays <= 2) toneKey = "soon";
  else toneKey = "ok";
  const tone = TASK_DEADLINE_DATE_BADGE.tones[toneKey];
  return `${TASK_DEADLINE_DATE_BADGE.base} ${tone}`;
}

/**
 * Text-only badge style (no background). Use when you want badges to
 * look like plain text but still use the badge rendering pipeline.
 */
export const TEXT_ONLY_BADGE_CLASS =
  "text-surface-700 dark:text-surface-200 font-normal";

/**
 * Company location badge styles:
 * - standard: default/main address badge (highlighted)
 * - typeText: typed address badges base style (color is applied separately)
 */
export const COMPANY_LOCATION_BADGES = {
  standard:
    "text-md font-medium rounded-full px-2 py-0.5 bg-primary/15 text-primary shrink-0",
  typeText: "text-md font-medium rounded-full px-2 py-0.5 shrink-0",
};

/**
 * Default badge for "any value as label" (e.g. sector name). Use when you want the cell value shown as a badge with one style.
 * Use: { key: 'sector_name', header: 'Sector', type: 'badge', defaultBadgeEntry: SECTOR_BADGE_DEFAULT, hideBadgeWhenEmpty: true, ... }
 */
export const SECTOR_BADGE_DEFAULT = {
  label: "—",
  class: PRESET_BADGE_CLASSES.info,
};

/**
 * Default badge style for subsidy themes in the companies table.
 */
export const SUBSIDY_BADGE_DEFAULT = {
  label: "—",
  class: PRESET_BADGE_CLASSES.purple,
};

/**
 * Template kind badges for project templates table.
 */
export const TEMPLATE_KIND_BADGES = {
  project: {
    label: "Project template",
    class: "bg-sky-100 text-sky-800 dark:bg-sky-900/30 dark:text-sky-300",
  },
  subsidy: {
    label: "Subsidie template",
    class:
      "bg-emerald-100 text-emerald-800 dark:bg-emerald-900/30 dark:text-emerald-300",
  },
};

/**
 * One primary style for all lifecycle phases (no per-phase colors).
 * Filled pill for `BaseStatusBadge` / B2CDataTable: use with `projectPhaseBadgeKey(phase)` + `PROJECT_PHASE_BADGES`.
 */
export const PROJECT_PHASE_BADGE_CLASS =
  "bg-primary/15 text-primary dark:bg-primary/20 dark:text-primary-300";

/** Text-only accent for phase labels (e.g. project dashboard header in `Index.vue`). */
export const PROJECT_PHASE_TEXT_CLASS = "text-primary dark:text-primary-300";

/**
 * @type {Record<string, { label: string, class: string }>}
 */
export const PROJECT_PHASE_BADGES = Object.fromEntries(
  PROJECT_PHASE_KEYS.map((key) => [
    key,
    { label: PROJECT_PHASE_LABELS[key], class: PROJECT_PHASE_BADGE_CLASS },
  ]),
);

/**
 * Normalize phase string for map lookup (lowercase trim).
 * @param {string|null|undefined} phase
 * @returns {string}
 */
export function projectPhaseBadgeKey(phase) {
  if (phase == null) return "";
  const s = String(phase).trim();
  return s ? s.toLowerCase() : "";
}

/**
 * Project dashboard: filled pill for **`projects.status`** under the title (**`Project/Dashboard/Index.vue`**
 * → **`BaseStatusBadge`** via **`defaultEntry.class`**). Currently **`PRESET_BADGE_CLASSES.success`**.
 * Dashboard phase row in **`Index.vue`** uses inline primary text classes, not this constant.
 */
export const PROJECT_STATUS_DASHBOARD_BADGE_CLASS =
  PRESET_BADGE_CLASSES.success;

/**
 * `projects.status` API values → Dutch labels (dashboard + tables). Same pill style for all.
 */
export const PROJECT_STATUS_BADGES = {
  open: { label: "Open", class: PROJECT_STATUS_DASHBOARD_BADGE_CLASS },
  in_progress: {
    label: "In uitvoering",
    class: PROJECT_STATUS_DASHBOARD_BADGE_CLASS,
  },
  on_hold: {
    label: "In de wacht",
    class: PROJECT_STATUS_DASHBOARD_BADGE_CLASS,
  },
  closed: { label: "Afgesloten", class: PROJECT_STATUS_DASHBOARD_BADGE_CLASS },
};

/**
 * Optional: different colors per sector. Keys = sector name (as returned by API).
 * Use: { key: 'sector_name', header: 'Sector', type: 'badge', valueMap: SECTOR_BADGES, defaultBadgeEntry: SECTOR_BADGE_DEFAULT, hideBadgeWhenEmpty: true, ... }
 * Add or change entries when you add sectors; use PRESET_BADGE_CLASSES for consistent colors.
 */
export const SECTOR_BADGES = {
  // Example – replace with your sector names from the sectors table:
  // Retail: { label: 'Retail', class: PRESET_BADGE_CLASSES.success },
  // IT: { label: 'IT', class: PRESET_BADGE_CLASSES.info },
  // Healthcare: { label: 'Healthcare', class: PRESET_BADGE_CLASSES.teal },
  // Manufacturing: { label: 'Manufacturing', class: PRESET_BADGE_CLASSES.warn },
  // Other: { label: 'Overig', class: PRESET_BADGE_CLASSES.secondary }
};
