import React, { useMemo, useCallback, memo } from "react";
import { Link } from "react-router";
import { useSkillCategoryManager } from "../../../../../services/Services";
import { SettingsListView } from "../../../../../components/business/views";
import { ViewButton, UIXThemeProvider, useUIXTheme } from "../../../../../components/UIX";
import { AcademicCapIcon } from "@heroicons/react/24/outline";
import { SKILL_CATEGORY_STATUS, SKILL_CATEGORY_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: SKILL_CATEGORY_STATUS.ACTIVE.toString(), label: SKILL_CATEGORY_STATUS_LABELS[SKILL_CATEGORY_STATUS.ACTIVE] },
        { value: SKILL_CATEGORY_STATUS.INACTIVE.toString(), label: SKILL_CATEGORY_STATUS_LABELS[SKILL_CATEGORY_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 SettingSkillCategoryListPage() {
  return (
    <UIXThemeProvider>
      <SettingSkillCategoryListPageContent />
    </UIXThemeProvider>
  );
}

const SettingSkillCategoryListPageContent = memo(function SettingSkillCategoryListPageContent() {
  const skillCategoryManager = useSkillCategoryManager();
  const { getThemeClasses } = useUIXTheme();

  // Memoize theme classes
  const themeClasses = useMemo(
    () => ({
      linkText: getThemeClasses('text-primary'),
      linkHover: getThemeClasses('text-primary-hover'),
      textMuted: getThemeClasses('text-muted'),
      badgeActiveBg: getThemeClasses('badge-active-bg'),
      badgeActiveText: getThemeClasses('badge-active-text'),
      badgeInactiveBg: getThemeClasses('badge-inactive-bg'),
      badgeInactiveText: getThemeClasses('badge-inactive-text'),
    }),
    [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: 'name',
      label: 'Name',
      render: (value, skillCategory) => (
        <Link
          to={`/admin/settings/skill-category/${skillCategory.id}/detail`}
          className={`${themeClasses.linkText} ${themeClasses.linkHover} font-medium`}
        >
          {skillCategory.name}
        </Link>
      )
    },
    {
      key: 'actions',
      label: 'Actions',
      align: 'right',
      render: (value, skillCategory) => (
        <ViewButton basePath="/admin/settings/skill-category" itemId={skillCategory.id} />
      )
    }
  ], [themeClasses]);

  return (
    <SettingsListView
      entityType="skillCategory"
      entityTypePlural="Skill Categories"
      entityIcon={AcademicCapIcon}
      entityManager={skillCategoryManager}
      basePath="/admin/settings/skill-category"
      createPath="/admin/settings/skill-category/create"
      title="Skill Categories"
      subtitle="Manage skill categorization and classification"
      columns={columns}
      searchPlaceholder="Search by name or description..."
      statusOptions={STATUS_OPTIONS}
      sortOptions={SORT_OPTIONS}
      onRowClick={handleRowClick}
    />
  );
});

SettingSkillCategoryListPageContent.displayName = 'SettingSkillCategoryListPageContent';

export default SettingSkillCategoryListPage;