import React, { useMemo, useCallback, memo } from "react";
import { Link } from "react-router";
import { useCertificationManager } from "../../../../../services/Services";
import { SettingsListView } from "../../../../../components/business/views";
import { ViewButton, UIXThemeProvider, useUIXTheme } from "../../../../../components/UIX";
import { ShieldCheckIcon } from "@heroicons/react/24/outline";
import { CERTIFICATION_STATUS, CERTIFICATION_STATUS_LABELS } from "../../../../../constants/Settings";
import { formatDateForDisplay } from "../../../../../services/Helpers/DateFormatter";

// Move static configuration outside component to prevent recreation
const STATUS_OPTIONS = [
  { value: "", label: "All Statuses" },
  { value: CERTIFICATION_STATUS.ACTIVE.toString(), label: CERTIFICATION_STATUS_LABELS[CERTIFICATION_STATUS.ACTIVE] },
  { value: CERTIFICATION_STATUS.INACTIVE.toString(), label: CERTIFICATION_STATUS_LABELS[CERTIFICATION_STATUS.INACTIVE] },
];

const SORT_OPTIONS = [
  { value: "name,ASC", label: "Name (A-Z)" },
  { value: "name,DESC", label: "Name (Z-A)" },
  { value: "created_at,DESC", label: "Created Date (Newest)" },
  { value: "created_at,ASC", label: "Created Date (Oldest)" },
];

function SettingCertificationListPage() {
  return (
    <UIXThemeProvider>
      <SettingCertificationListPageContent />
    </UIXThemeProvider>
  );
}

const SettingCertificationListPageContent = memo(function SettingCertificationListPageContent() {
  const certificationManager = useCertificationManager();
  const { getThemeClasses } = useUIXTheme();

  // Memoize theme classes
  const themeClasses = useMemo(
    () => ({
      linkText: getThemeClasses('text-primary'),
      linkHover: getThemeClasses('text-primary-hover'),
      badgeActiveBg: getThemeClasses('badge-active-bg'),
      badgeActiveText: getThemeClasses('badge-active-text'),
      badgeInactiveBg: getThemeClasses('badge-inactive-bg'),
      badgeInactiveText: getThemeClasses('badge-inactive-text'),
      textMuted: getThemeClasses('text-muted'),
    }),
    [getThemeClasses],
  );

  // Memoize row click handler
  const handleRowClick = useCallback((item) => {
    return `${window.location.pathname}/${item.id}/detail`;
  }, []);

  // Memoize table columns with theme-aware styling
  const columns = useMemo(() => [
    {
      key: 'specializationName',
      label: 'Specialization',
      render: (value, certification) => {
        const specialization = certification.specializationName || 'N/A';
        const truncated = specialization.length > 20
          ? `${specialization.substring(0, 20)}...`
          : specialization;
        return (
          <span title={specialization}>
            {truncated}
          </span>
        );
      }
    },
    {
      key: 'name',
      label: 'Name',
      render: (value, certification) => (
        <Link
          to={`/admin/settings/certification/${certification.id}/detail`}
          className={`font-medium ${themeClasses.linkText} ${themeClasses.linkHover}`}
        >
          {certification.name}
        </Link>
      )
    },
    {
      key: 'actions',
      label: 'Actions',
      align: 'right',
      render: (value, certification) => (
        <ViewButton basePath="/admin/settings/certification" itemId={certification.id} />
      )
    }
  ], [themeClasses]);

  return (
    <SettingsListView
      entityType="certification"
      entityTypePlural="Certifications"
      entityIcon={ShieldCheckIcon}
      entityManager={certificationManager}
      basePath="/admin/settings/certification"
      createPath="/admin/settings/certification/create"
      title="Certifications"
      subtitle="Manage professional certifications and qualifications"
      columns={columns}
      searchPlaceholder="Search by name or description..."
      statusOptions={STATUS_OPTIONS}
      sortOptions={SORT_OPTIONS}
      onRowClick={handleRowClick}
    />
  );
});
SettingCertificationListPageContent.displayName = 'SettingCertificationListPageContent';

export default SettingCertificationListPage;
