import { useEffect, useRef, useState } from "react";
import type { ApiSettings, FileConnectorScope, FileConnectorTargetOption, FilesConnectorsUiCapability } from "@govoplan/core-webui";
import { AdminPageLayout, Card, adminErrorMessage, useDeltaWatermarks, usePlatformUiCapability } from "@govoplan/core-webui";
import { fetchGroupsDelta, fetchUsersDelta, type GroupSummary, type UserAdminItem } from "../../api/admin";
import { loadDeltaRows } from "./utils/deltaRows";

type Props = {
  settings: ApiSettings;
  scopeType: Extract<FileConnectorScope, "user" | "group">;
  canWrite: boolean;
};

const copy: Record<Props["scopeType"], {title: string;description: string;targetLabel: string;panelTitle: string;}> = {
  user: {
    title: "i18n:govoplan-access.user_file_connections.996444a0",
    description: "i18n:govoplan-access.user_scoped_file_server_connections_and_credenti.ae7a4be2",
    targetLabel: "i18n:govoplan-access.user.9f8a2389",
    panelTitle: "i18n:govoplan-access.user_file_connections.996444a0"
  },
  group: {
    title: "i18n:govoplan-access.group_file_connections.73985bda",
    description: "i18n:govoplan-access.group_scoped_file_server_connections_and_credent.f32a51bc",
    targetLabel: "i18n:govoplan-access.group.171a0606",
    panelTitle: "i18n:govoplan-access.group_file_connections.73985bda"
  }
};

export default function FileConnectorsPanel({ settings, scopeType, canWrite }: Props) {
  const fileConnectorsUi = usePlatformUiCapability<FilesConnectorsUiCapability>("files.connectors");
  const FileConnectorScopeManager = fileConnectorsUi?.FileConnectorScopeManager ?? null;
  const [targets, setTargets] = useState<FileConnectorTargetOption[]>([]);
  const usersRef = useRef<UserAdminItem[]>([]);
  const groupsRef = useRef<GroupSummary[]>([]);
  const { getDeltaWatermark, setDeltaWatermark, resetDeltaWatermark } = useDeltaWatermarks();
  const [loadingTargets, setLoadingTargets] = useState(Boolean(FileConnectorScopeManager));
  const [targetError, setTargetError] = useState("");

  useEffect(() => {
    if (!FileConnectorScopeManager) {
      setTargets([]);
      setLoadingTargets(false);
      setTargetError("");
      return;
    }
    usersRef.current = [];
    groupsRef.current = [];
    resetDeltaWatermark();
    void loadTargets();
  }, [settings.accessToken, settings.apiBaseUrl, settings.apiKey, scopeType, FileConnectorScopeManager, resetDeltaWatermark]);

  async function loadTargets() {
    setLoadingTargets(true);
    setTargetError("");
    try {
      if (scopeType === "user") {
        const users = await loadDeltaRows(usersRef.current, "access:file-connector-users", getDeltaWatermark, setDeltaWatermark, (since) => fetchUsersDelta(settings, { since }), (response) => response.users, (user) => user.id, "access_user", sortUsers);
        usersRef.current = users;
        setTargets(users.map((user) => ({
          id: user.id,
          label: user.display_name || user.email,
          secondary: user.display_name ? user.email : null
        })));
      } else {
        const groups = await loadDeltaRows(groupsRef.current, "access:file-connector-groups", getDeltaWatermark, setDeltaWatermark, (since) => fetchGroupsDelta(settings, { since }), (response) => response.groups, (group) => group.id, "access_group", sortGroups);
        groupsRef.current = groups;
        setTargets(groups.map((group) => ({
          id: group.id,
          label: group.name,
          secondary: group.slug
        })));
      }
    } catch (err) {
      setTargets([]);
      setTargetError(adminErrorMessage(err));
    } finally {
      setLoadingTargets(false);
    }
  }

  const labels = copy[scopeType];

  if (!FileConnectorScopeManager) {
    return (
      <AdminPageLayout title={labels.title} description={labels.description}>
        <Card title="i18n:govoplan-access.files_module_unavailable.0ee90db1">
          <p className="muted">i18n:govoplan-access.install_and_enable_the_files_module_to_manage_fi.f842c153</p>
        </Card>
      </AdminPageLayout>);

  }

  return (
    <AdminPageLayout title={labels.title} description={labels.description} loading={loadingTargets} error={targetError}>
      <FileConnectorScopeManager
        settings={settings}
        scopeType={scopeType}
        targetOptions={targets}
        targetLabel={labels.targetLabel}
        title={labels.panelTitle}
        canWrite={canWrite} />

    </AdminPageLayout>);

}

function sortUsers(left: UserAdminItem, right: UserAdminItem): number {
  return left.email.localeCompare(right.email);
}

function sortGroups(left: GroupSummary, right: GroupSummary): number {
  return left.name.localeCompare(right.name) || left.slug.localeCompare(right.slug);
}
