// #region BreadcrumbsContractTest:Module [TYPE Function]
// @UX_STATE: Loading -> Default
// @PURPOSE: Contract-focused unit tests for Breadcrumbs.svelte logic and UX annotations
// @LAYER UI
// @RELATION DEPENDS_ON -> [EXT:frontend:Breadcrumbs]

import { describe, it, expect } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const COMPONENT_PATH = path.resolve(
  process.cwd(),
  'src/lib/components/layout/Breadcrumbs.svelte'
);

function getBreadcrumbs(pathname, maxVisible = 3) {
  const segments = pathname.split('/').filter(Boolean);
  const allItems = [{ label: 'Home', path: '/' }];

  let currentPath = '';
  segments.forEach((segment, index) => {
    currentPath += `/${segment}`;
    const label = formatBreadcrumbLabel(segment);
    allItems.push({
      label,
      path: currentPath,
      isLast: index === segments.length - 1
    });
  });

  if (allItems.length > maxVisible) {
    const firstItem = allItems[0];
    const itemsToShow = [];
    itemsToShow.push(firstItem);
    itemsToShow.push({ isEllipsis: true });

    const startFromIndex = allItems.length - (maxVisible - 1);
    for (let i = startFromIndex; i < allItems.length; i++) {
      itemsToShow.push(allItems[i]);
    }
    return itemsToShow;
  }

  return allItems;
}

function formatBreadcrumbLabel(segment) {
  const specialCases = {
    dashboards: 'Dashboards',
    datasets: 'Datasets',
    storage: 'Storage',
    admin: 'Admin',
    settings: 'Settings',
    git: 'Git'
  };

  if (specialCases[segment]) {
    return specialCases[segment];
  }

  return segment
    .split('-')
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}

describe('Breadcrumbs Component Contract & Logic', () => {
  it('contains required UX tags and current dependency contract', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('@UX_STATE: Idle');
    expect(source).toContain('@UX_FEEDBACK');
    expect(source).toContain('@UX_RECOVERY');
    expect(source).toContain('@RELATION BINDS_TO -> [EXT:frontend:i18n]');
    expect(source).toContain('import { page } from "$app/state"');
  });

  it('returns Home for root path (Short-Path UX state)', () => {
    const result = getBreadcrumbs('/', 3);

    expect(result).toEqual([{ label: 'Home', path: '/' }]);
  });

  it('maps known segments to expected labels', () => {
    expect(formatBreadcrumbLabel('dashboards')).toBe('Dashboards');
    expect(formatBreadcrumbLabel('datasets')).toBe('Datasets');
    expect(formatBreadcrumbLabel('settings')).toBe('Settings');
  });

  it('formats unknown kebab-case segment to title case', () => {
    expect(formatBreadcrumbLabel('data-quality-rules')).toBe('Data Quality Rules');
  });

  it('truncates long paths with ellipsis and keeps tail segments', () => {
    const result = getBreadcrumbs('/dashboards/segment-a/segment-b/segment-c', 3);

    expect(result[0]).toEqual({ label: 'Home', path: '/' });
    expect(result[1]).toEqual({ isEllipsis: true });
    const lastItem = result[result.length - 1];
    expect('label' in lastItem && lastItem.label).toBe('Segment C');
    expect(result.length).toBe(4);
  });
});

// #endregion BreadcrumbsContractTest:Module
