import { useEffect, useState } from "react";
import { FeatureSettingsWindow } from "@nodedc/ui-react";
import { FoundryCodexAgentSettings } from "./FoundryCodexAgentSettings.js";
import { FoundryMapProviderSettings } from "./FoundryMapProviderSettings.js";

interface FoundrySettingsModalProps {
  open: boolean;
  isAdmin: boolean;
  onClose: () => void;
}

type FoundrySettingsSection = "codex-agent-api" | "map-providers";

export function FoundrySettingsModal({ open, isAdmin, onClose }: FoundrySettingsModalProps) {
  const [activeSection, setActiveSection] = useState<FoundrySettingsSection>("codex-agent-api");
  useEffect(() => {
    if (!isAdmin && activeSection === "map-providers") setActiveSection("codex-agent-api");
  }, [activeSection, isAdmin]);
  const sections = [
    { id: "codex-agent-api" as const, label: "Codex Agent API", group: "Features", icon: "network" as const },
    ...(isAdmin ? [{
      id: "map-providers" as const,
      label: "Картографические источники",
      group: "Admin",
      icon: "globe" as const,
    }] : []),
  ];
  return (
    <FeatureSettingsWindow
      open={open}
      title="Настройки Foundry"
      subtitle="NODE DC / Codex Agent API"
      identity={{ title: "NODE.DC Foundry", subtitle: "Текущий пользователь", avatarLabel: "NF" }}
      sections={sections}
      activeSection={activeSection}
      onSectionChange={setActiveSection}
      onClose={onClose}
    >
      {activeSection === "map-providers" && isAdmin
        ? <FoundryMapProviderSettings />
        : <FoundryCodexAgentSettings />}
    </FeatureSettingsWindow>
  );
}
