'use client';

import { usePathname } from 'next/navigation';
import { useMemo } from 'react';

type BreadcrumbItem = {
  title: string;
  link: string;
};

const routeMapping: Record<string, BreadcrumbItem[]> = {
  '/dashboard': [
    { title: 'Main Menu', link: '' },
    { title: 'Dashboard', link: '/dashboard' },
  ],
  '/job-management/create': [
    { title: 'Main Menu', link: '' },
    { title: 'Job Management', link: '/job-management' },
    { title: 'Create Job', link: '/job-management/create' },
  ],
  '/job-management/jobqueue': [
    { title: 'Main Menu', link: '' },
    { title: 'Job Queue', link: '/job-management/jobqueue' },
  ],
  '/data-management/result': [
    { title: 'Main Menu', link: '' },
    { title: 'Results', link: '/data-management/result' },
  ],
  '/data-management/result/[id]/[job]': [
    { title: 'Main Menu', link: '' },
    { title: 'Results', link: '/data-management/result' },
    { title: 'UiUX Designer', link: '' },
  ],
  '/data-management/contact/verified': [
    { title: 'Main Menu', link: '' },
    { title: 'Verified Contacts', link: '' },
  ],
  '/data-management/contact/nonVerified': [
    { title: 'Main Menu', link: '' },
    { title: 'Non Verified Contacts', link: '' },
  ],
  '/credit': [
    { title: 'Main Menu', link: '' },
    { title: 'Credit management', link: '' },
    { title: 'Credit history', link: '' },
  ],
  '/profile': [
    { title: 'Profile', link: '' },
    { title: 'Settings', link: '' },
  ],
  '/profile/subscription': [
    { title: 'Profile', link: '' },
    { title: 'Subscription', link: '' },
  ],
  '/profile/subscription/billing-history': [
    { title: 'Profile', link: '' },
    { title: 'Subscription', link: '' },
    { title: 'Billing history', link: '' },

  ],
  '/settings/notification': [
    { title: 'Profile', link: '/profile' },
    { title: 'Notification settings', link: '' },

  ],
  '/support': [
    { title: 'Main Menu', link: '' },
    { title: 'Contact Us', link: '' },

  ],
  '/tutorials-and-guides': [
    { title: 'Profile', link: '' },
    { title: 'Support', link: '' },
    { title: 'Tutorials & guides', link: '' },

  ],
  '/credit/history': [
    { title: 'Main Menu', link: '' },
    { title: 'Credit management', link: '' },
    { title: 'Credit history', link: '' },

  ],
  // automation job....
  '/automation/jobListing': [
    { title: 'Main Menu', link: '' },
    { title: 'Automations', link: '' },
    { title: 'Job listing automation', link: '' },

  ],
  '/automation/nonJobListing': [
    { title: 'Main Menu', link: '' },
    { title: 'Automations', link: '' },
    { title: 'Non-job listing automation', link: '' },

  ],
  '/automation/nonJobListing/generate': [
    { title: 'Main Menu', link: '' },
    { title: 'Automations', link: '' },
    { title: 'Non-job listing automation', link: '/automation/nonJobListing' },
    { title: 'Generate summary', link: '' },

  ],

  //list related path
  '/lists/my_list': [
    { title: 'Main Menu', link: '' },
    { title: 'My lists', link: '' },

  ],
  '/lists/shared': [
    { title: 'Main Menu', link: '' },
    { title: 'Lists', link: '' },
    { title: 'Shared with me', link: '' },

  ],
  '/data-management/result/generate/textField': [
    { title: 'Main Menu', link: '' },
    { title: 'Results', link: '/data-management/result/page/1?limit=10' },
    { title: 'Job listing automation', link: '' },
  ],
  '/data-management/result/generate/uploadFile': [
    { title: 'Main Menu', link: '' },
    { title: 'Results', link: '/data-management/result/page/1?limit=10' },
    { title: 'Job listing automation', link: '' },
  ],
  '/data-management/result/generate/automatic': [
    { title: 'Main Menu', link: '' },
    { title: 'Results', link: '/data-management/result/page/1?limit=10' },
    { title: 'Job listing automation', link: '' },
  ],
  '/notifications': [
    { title: 'Notifications', link: '' },
    { title: 'All', link: '' },
  ],
  '/tutorials-and-guides/id/view': [
    { title: 'Profile', link: '' },
    { title: 'Support', link: '/tutorials-and-guides' },
    { title: 'View', link: '' },
  ],
  '/lists/shared/id': [
    { title: 'Main Menu', link: '' },
    { title: 'Shared with me', link: '/lists/shared/page/1?limit=10' },
    { title: 'View', link: '' },
  ],
  '/customer-queries-list': [
    { title: 'Snap & Order', link: '/customer-queries-list' },
  ],
  '/grocery-pos': [
    { title: 'POS', link: '/grocery-pos' },
  ],
  '/grocery-pos-order-list': [
    { title: 'POS Order List', link: '/grocery-pos-order-list' },
  ],
  '/point-of-sale': [
    { title: 'POS', link: '/point-of-sale' },
  ],
  '/pos-order-list': [
    { title: 'POS Order List', link: '/pos-order-list' },
  ]
};



export function useBreadcrumbs() {
  const pathname = usePathname();

  const breadcrumbs = useMemo(() => {
    let adjustedPathname = pathname;

    if (adjustedPathname.includes('?')) {
      adjustedPathname = adjustedPathname.split('?')[0];
    }

    adjustedPathname = adjustedPathname
      .split('/')
      .map((segment) => (/[a-fA-F0-9]{24}/.test(segment) ? 'id' : segment))
      .join('/');

    if (adjustedPathname.includes('/page')) {
      adjustedPathname = adjustedPathname.split('/page')[0];
    }

    if (routeMapping[adjustedPathname]) {
      return routeMapping[adjustedPathname];
    }

    const segments = pathname.split('/').filter(Boolean);
    const dynamicSegments = segments.map((segment, index) => {
      if (/[a-fA-F0-9]{24}/.test(segment)) return 'id';
      return segment;
    });

    if (
      dynamicSegments.includes('data-management') &&
      dynamicSegments.includes('result') &&
      dynamicSegments.length >= 4
    ) {
      const jobTitle = decodeURIComponent(segments[3]);
      return [
        { title: 'Main Menu', link: '' },
        { title: 'Results', link: '/data-management/result/page/1?limit=10' },
        { title: jobTitle, link: '' },
      ];
    }

    // Handling the `lists/my_list/:id/page/:pageNumber` route
    if (
      dynamicSegments.includes('lists') &&
      dynamicSegments.includes('my_list') &&
      dynamicSegments.length >= 3
    ) {
      const listId = segments[2]; // Dynamic ID segment
      const pageNumber = segments[4]; // Page number segment
      return [
        { title: 'Main Menu', link: '' },
        { title: 'Lists', link: '/lists/my_list/page/1?limit=10' },
        { title: 'My lists', link: '' },
      ];
    }

    return segments.map((segment, index) => {
      const path = `/${segments.slice(0, index + 1).join('/')}`;
      return {
        title: segment.charAt(0).toUpperCase() + segment.slice(1).replace(/%20/g, ' '),
        link: path,
      };
    });
  }, [pathname]);

  return breadcrumbs;
}
