import {
  CloudSyncOutlined,
  DeleteOutlined,
  EditOutlined,
  FolderOpenOutlined,
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  ReloadOutlined,
} from "@ant-design/icons";
import { useCallback, useMemo, useState } from "react";
import type { WebProjectSummary } from "../../types";

interface ProjectSidebarProps {
  projects: WebProjectSummary[];
  activeId: string | null;
  collapsed: boolean;
  filterMode?: "chat" | "image" | "video";
  loading?: boolean;
  error?: string | null;
  onToggle: () => void;
  onSelect: (id: string) => void;
  onRefresh: () => void;
  onRename: (id: string, title: string) => void;
  onDelete: (id: string) => void;
}

function formatRelativeTime(dateStr: string): string {
  const relativeMatch = dateStr.trim().match(/^(\d+)\s*(s|sec|secs|second|seconds|m|min|mins|minute|minutes|h|hr|hrs|hour|hours|d|day|days|w|week|weeks|mo|month|months|y|yr|year|years)\s+ago$/i);
  if (relativeMatch) {
    const value = Number(relativeMatch[1]);
    const unit = relativeMatch[2].toLowerCase();
    if (unit.startsWith("s")) return "刚刚";
    if (unit === "m" || unit.startsWith("min")) return `${value} 分钟前`;
    if (unit === "h" || unit.startsWith("hr") || unit.startsWith("hour")) return `${value} 小时前`;
    if (unit === "d" || unit.startsWith("day")) return `${value} 天前`;
    if (unit === "w" || unit.startsWith("week")) return `${value} 周前`;
    if (unit === "mo" || unit.startsWith("month")) return `${value} 个月前`;
    if (unit === "y" || unit.startsWith("yr") || unit.startsWith("year")) return `${value} 年前`;
  }

  const then = new Date(dateStr).getTime();
  if (!Number.isFinite(then)) return dateStr;
  const diff = Date.now() - then;
  if (diff < 60_000) return "刚刚";
  if (diff < 3_600_000) return `${Math.floor(diff / 60_000)} 分钟前`;
  if (diff < 86_400_000) return `${Math.floor(diff / 3_600_000)} 小时前`;
  if (diff < 604_800_000) return `${Math.floor(diff / 86_400_000)} 天前`;
  return new Date(dateStr).toLocaleDateString("zh-CN");
}

export default function ProjectSidebar({
  projects,
  activeId,
  collapsed,
  filterMode,
  loading,
  error,
  onToggle,
  onSelect,
  onRefresh,
  onRename,
  onDelete,
}: ProjectSidebarProps) {
  const [editingId, setEditingId] = useState<string | null>(null);
  const [editValue, setEditValue] = useState("");

  const filteredProjects = useMemo(
    () => filterMode ? projects.filter((p) => p.mode === filterMode) : projects,
    [projects, filterMode],
  );

  const startRename = useCallback((project: WebProjectSummary) => {
    setEditingId(project.id);
    setEditValue(project.name);
  }, []);

  const commitRename = useCallback(() => {
    if (editingId && editValue.trim()) {
      onRename(editingId, editValue.trim());
    }
    setEditingId(null);
  }, [editValue, editingId, onRename]);

  return (
    <aside className={`conversation-sidebar${collapsed ? " is-collapsed" : ""}`}>
      <div className="conversation-sidebar__header">
        <button
          type="button"
          className="conversation-sidebar__toggle"
          onClick={onToggle}
          aria-label={collapsed ? "Expand project sidebar" : "Collapse project sidebar"}
        >
          {collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
        </button>
        {!collapsed && (
          <div className="conversation-sidebar__header-actions">
            <button type="button" className="conversation-sidebar__new" onClick={() => onSelect("")}>
              <FolderOpenOutlined /> 新对话
            </button>
            <button type="button" className="conversation-sidebar__icon-button" onClick={onRefresh} aria-label="刷新记录">
              <ReloadOutlined />
            </button>
          </div>
        )}
      </div>

      {!collapsed && (
        <div className="conversation-sidebar__list">
          {error ? (
            <div className="conversation-sidebar__empty">
              <CloudSyncOutlined />
              <span>Server data failed to load</span>
            </div>
          ) : filteredProjects.length === 0 ? (
            <div className="conversation-sidebar__empty">
              <FolderOpenOutlined />
              <span>{loading ? "正在加载记录..." : "暂无该类型记录"}</span>
            </div>
          ) : (
            filteredProjects.map((project) => (
              <div
                key={project.id}
                className={`conversation-sidebar__item${project.id === activeId ? " is-active" : ""}`}
              >
                {editingId === project.id ? (
                  <input
                    className="conversation-sidebar__rename-input"
                    value={editValue}
                    onChange={(event) => setEditValue(event.target.value)}
                    onBlur={commitRename}
                    onKeyDown={(event) => {
                      if (event.key === "Enter") commitRename();
                      if (event.key === "Escape") setEditingId(null);
                    }}
                    autoFocus
                  />
                ) : (
                  <button
                    type="button"
                    className="conversation-sidebar__item-main"
                    onClick={() => onSelect(project.id)}
                  >
                    <span className="conversation-sidebar__item-title">{project.name}</span>
                    <span className="conversation-sidebar__item-time">
                      {project.source === "server" ? formatRelativeTime(project.updatedAt) : "preview"}
                    </span>
                  </button>
                )}
                <div className="conversation-sidebar__item-actions">
                  <button type="button" aria-label="Rename project" onClick={() => startRename(project)}>
                    <EditOutlined />
                  </button>
                  <button type="button" aria-label="Delete project" onClick={() => onDelete(project.id)}>
                    <DeleteOutlined />
                  </button>
                </div>
              </div>
            ))
          )}
        </div>
      )}
    </aside>
  );
}
