import React, { useMemo, useCallback } from "react";
import { Link } from "react-router";
import { useTagManager } from "../../../../../services/Services";
import { SettingsListView } from "../../../../../components/business/views";
import { ViewButton, UIXThemeProvider, useUIXTheme } from "../../../../../components/UIX";
import { TagIcon } from "@heroicons/react/24/outline";
import { TAG_STATUS, TAG_STATUS_LABELS } from "../../../../../constants/Settings";
import { formatDateForDisplay } from "../../../../../services/Helpers/DateFormatter";

function SettingTagListPage() {
  return (
    <UIXThemeProvider>
      <SettingTagListPageContent />
    </UIXThemeProvider>
  );
}

function SettingTagListPageContent() {
  const tagManager = useTagManager();
  const { getThemeClasses } = useUIXTheme();

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

  // Memoize theme classes
  const themeClasses = useMemo(
    () => ({
      linkText: getThemeClasses('text-primary'),
      linkHover: getThemeClasses('hover:text-accent'),
      textMuted: getThemeClasses('text-muted'),
    }),
    [getThemeClasses],
  );

  // Define table columns - now moved after themeClasses
  const columns = useMemo(() => [
    {
      key: 'text',
      label: 'Name',
      render: (value, tag) => (
        <Link
          to={`/admin/settings/tag/${tag.id}/detail`}
          className={`${themeClasses.linkText} ${themeClasses.linkHover} font-medium`}
        >
          {tag.text}
        </Link>
      )
    },
    {
      key: 'description',
      label: 'Description',
      render: (value, tag) => (
        <div className="max-w-xs truncate">
          {tag.description || (
            <span className={`${themeClasses.textMuted} italic`}>—</span>
          )}
        </div>
      )
    },
    {
      key: 'status',
      label: 'Status',
      render: (value, tag) => {
        const isActive = tag.status === TAG_STATUS.ACTIVE;
        return (
          <span
            className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${
              isActive ? getThemeClasses('status-active') : getThemeClasses('status-inactive')
            }`}
          >
            {TAG_STATUS_LABELS[tag.status] || "Unknown"}
          </span>
        );
      }
    },
    {
      key: 'createdAt',
      label: 'Created At',
      render: (value, tag) => formatDateForDisplay(tag.createdAt) || "—"
    },
    {
      key: 'actions',
      label: 'Actions',
      align: 'right',
      render: (value, tag) => (
        <ViewButton basePath="/admin/settings/tag" itemId={tag.id} />
      )
    }
  ], [themeClasses]);

  // Define status options
  const statusOptions = [
    { value: "", label: "All Statuses" },
    { value: TAG_STATUS.ACTIVE.toString(), label: TAG_STATUS_LABELS[TAG_STATUS.ACTIVE] },
    { value: TAG_STATUS.INACTIVE.toString(), label: TAG_STATUS_LABELS[TAG_STATUS.INACTIVE] },
  ];

  // Define sort options
  const sortOptions = [
    { value: "text,ASC", label: "Name (A-Z)" },
    { value: "text,DESC", label: "Name (Z-A)" },
    { value: "created_at,DESC", label: "Created Date (Newest)" },
    { value: "created_at,ASC", label: "Created Date (Oldest)" },
  ];

  return (
    <SettingsListView
      entityType="tag"
      entityTypePlural="Tags"
      entityIcon={TagIcon}
      entityManager={tagManager}
      basePath="/admin/settings/tag"
      createPath="/admin/settings/tag/create"
      title="Tags"
      subtitle="Manage tag categories and labels for events and resources"
      columns={columns}
      searchPlaceholder="Search by name or description..."
      statusOptions={statusOptions}
      sortOptions={sortOptions}
      onRowClick={handleRowClick}
    />
  );
}

export default SettingTagListPage;