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

const SORT_OPTIONS = [{ value: "startDate,DESC", label: "Start Date (Newest)" },
        { value: "startDate,ASC", label: "Start Date (Oldest)" },
        { value: "speakerName,ASC", label: "Speaker (A-Z)" },
        { value: "speakerName,DESC", label: "Speaker (Z-A)" },];

function SettingSpeakerAwayLogListPage() {
  return (
    <UIXThemeProvider>
      <SettingSpeakerAwayLogListPageContent />
    </UIXThemeProvider>
  );
}

const SettingSpeakerAwayLogListPageContent = memo(function SettingSpeakerAwayLogListPageContent() {
  const speakerAwayLogManager = useSpeakerAwayLogManager();
  const { getThemeClasses } = useUIXTheme();

  // Memoize theme classes
  const themeClasses = useMemo(
    () => ({
      linkText: getThemeClasses('text-primary'),
      linkHover: getThemeClasses('text-primary-hover'),
      textWarning: getThemeClasses('text-warning'),
      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: 'speakerName',
      label: 'Speaker',
      render: (value, awayLog) => (
        <Link
          to={`/admin/settings/speaker-away-log/${awayLog.id}/detail`}
          className={`${themeClasses.linkText} ${themeClasses.linkHover} font-medium`}
        >
          {awayLog.speakerName || awayLog.speaker?.name || "—"}
        </Link>
      )
    },
    {
      key: 'startDate',
      label: 'Start Date',
      render: (value, awayLog) => formatDateForDisplay(awayLog.startDate) || "—"
    },
    {
      key: 'untilFurtherNotice',
      label: 'End Date',
      render: (value, awayLog) => {
        if (awayLog.untilFurtherNotice === 1) {
          return <span className={`${themeClasses.textWarning} font-medium`}>Until Further Notice</span>;
        }
        return formatDateForDisplay(awayLog.untilDate) || "—";
      }
    },
    {
      key: 'actions',
      label: 'Actions',
      align: 'right',
      render: (value, awayLog) => (
        <ViewButton basePath="/admin/settings/speaker-away-log" itemId={awayLog.id} />
      )
    }
  ], [themeClasses]);

  return (
    <SettingsListView
      entityType="speakerAwayLog"
      entityTypePlural="Speaker Away Logs"
      entityIcon={ClockIcon}
      entityManager={speakerAwayLogManager}
      basePath="/admin/settings/speaker-away-log"
      createPath="/admin/settings/speaker-away-log/create"
      title="Speaker Away Logs"
      subtitle="Manage speaker availability and absence tracking"
      columns={columns}
      searchPlaceholder="Search by speaker name..."
      sortOptions={SORT_OPTIONS}
      onRowClick={handleRowClick}
      defaultSort="startDate"
      defaultSortOrder="DESC"
    />
  );
});

SettingSpeakerAwayLogListPageContent.displayName = 'SettingSpeakerAwayLogListPageContent';

export default SettingSpeakerAwayLogListPage;