/**
 * Copyright (c) 2023-present Plane Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import { Fragment, useState } from "react";
import type { ReactNode } from "react";
import { Dialog, Transition } from "@headlessui/react";
import useSWR from "swr";
import { Loader, ShieldCheck, Trash2, UsersRound, X } from "lucide-react";
// plane imports
import { Button } from "@plane/propel/button";
import { setToast, TOAST_TYPE } from "@plane/propel/toast";
import { InstanceWorkspaceService } from "@plane/services";
import type { TInstanceWorkspaceFeature, TInstanceWorkspaceMember } from "@plane/types";
import { ToggleSwitch } from "@plane/ui";
import { cn } from "@plane/utils";
// hooks
import { useWorkspace } from "@/hooks/store";

type TWorkspaceAdminModalProps = {
  isOpen: boolean;
  onClose: () => void;
  workspaceId: string | null;
};

const instanceWorkspaceService = new InstanceWorkspaceService();

const ROLE_OPTIONS = [
  { label: "Гость", value: 5 },
  { label: "Участник", value: 15 },
  { label: "Администратор", value: 20 },
];

const ROLE_LABELS: Record<number, string> = {
  5: "Гость",
  15: "Участник",
  20: "Администратор",
};

const ACCESS_MODE_LABELS: Record<TInstanceWorkspaceFeature["access_mode"], string> = {
  all_workspace_members: "Весь воркспейс",
  admins_only: "Только админы",
  selected_projects: "По контурам",
  selected_members: "По людям",
};

function getMemberName(member: TInstanceWorkspaceMember) {
  return member.member.display_name || member.member.email || "Пользователь";
}

function getErrorMessage(error: unknown, fallback: string) {
  if (error && typeof error === "object" && "error" in error && typeof error.error === "string") return error.error;
  return fallback;
}

function WorkspaceModalShell(props: TWorkspaceAdminModalProps & { children: ReactNode; title: string; icon: ReactNode }) {
  const { children, icon, isOpen, onClose, title, workspaceId } = props;
  const { getWorkspaceById } = useWorkspace();
  const workspace = workspaceId ? getWorkspaceById(workspaceId) : undefined;

  return (
    <Transition.Root show={isOpen} as={Fragment}>
      <Dialog as="div" className="relative z-40" onClose={onClose}>
        <Transition.Child
          as={Fragment}
          enter="ease-out duration-200"
          enterFrom="opacity-0"
          enterTo="opacity-100"
          leave="ease-in duration-150"
          leaveFrom="opacity-100"
          leaveTo="opacity-0"
        >
          <div className="fixed inset-0 bg-black/70 backdrop-blur-sm" />
        </Transition.Child>

        <div className="fixed inset-0 overflow-y-auto">
          <div className="flex min-h-full items-center justify-center p-4">
            <Transition.Child
              as={Fragment}
              enter="ease-out duration-200"
              enterFrom="translate-y-2 opacity-0 scale-95"
              enterTo="translate-y-0 opacity-100 scale-100"
              leave="ease-in duration-150"
              leaveFrom="translate-y-0 opacity-100 scale-100"
              leaveTo="translate-y-2 opacity-0 scale-95"
            >
              <Dialog.Panel className="nodedc-glass-modal relative w-full max-w-5xl overflow-hidden rounded-[1.75rem] bg-surface-1 text-left shadow-raised-200 transition-all">
                <div className="flex items-start justify-between gap-4 border-b border-subtle p-5">
                  <div className="flex min-w-0 items-start gap-3">
                    <span className="flex h-10 w-10 shrink-0 items-center justify-center rounded-2xl bg-layer-2 text-accent-primary">
                      {icon}
                    </span>
                    <div className="min-w-0">
                      <Dialog.Title as="h3" className="truncate text-18 font-semibold text-primary">
                        {title}
                      </Dialog.Title>
                      <div className="mt-1 truncate text-13 text-secondary">
                        {workspace ? `${workspace.name} / [${workspace.slug}]` : "Воркспейс"}
                      </div>
                    </div>
                  </div>
                  <button
                    type="button"
                    onClick={onClose}
                    className="nodedc-settings-secondary-button flex h-10 min-h-10 w-10 shrink-0 items-center justify-center rounded-full p-0 !px-0"
                    aria-label="Закрыть"
                  >
                    <X className="h-4 w-4" />
                  </button>
                </div>
                <div className="max-h-[72vh] overflow-y-auto p-5">{children}</div>
              </Dialog.Panel>
            </Transition.Child>
          </div>
        </div>
      </Dialog>
    </Transition.Root>
  );
}

export function WorkspaceMembersModal(props: TWorkspaceAdminModalProps) {
  const { isOpen, onClose, workspaceId } = props;
  const [mutatingMemberId, setMutatingMemberId] = useState<string | null>(null);
  const { data, isLoading, mutate } = useSWR(
    isOpen && workspaceId ? ["INSTANCE_WORKSPACE_MEMBERS", workspaceId] : null,
    () => instanceWorkspaceService.listMembers(workspaceId as string)
  );

  const handleRoleChange = async (workspaceMemberId: string, role: number) => {
    if (!workspaceId) return;
    setMutatingMemberId(workspaceMemberId);
    try {
      await instanceWorkspaceService.updateMemberRole(workspaceId, workspaceMemberId, role);
      await mutate();
      setToast({ type: TOAST_TYPE.SUCCESS, title: "Роль участника обновлена" });
    } catch (error) {
      setToast({
        type: TOAST_TYPE.ERROR,
        title: "Не удалось обновить роль",
        message: getErrorMessage(error, "Проверьте ограничения по администраторам воркспейса и проектов."),
      });
    } finally {
      setMutatingMemberId(null);
    }
  };

  const handleRemove = async (workspaceMember: TInstanceWorkspaceMember) => {
    if (!workspaceId) return;
    const confirmed = window.confirm(`Отключить ${getMemberName(workspaceMember)} от этого воркспейса и его проектов?`);
    if (!confirmed) return;

    setMutatingMemberId(workspaceMember.id);
    try {
      await instanceWorkspaceService.removeMember(workspaceId, workspaceMember.id);
      await mutate();
      setToast({ type: TOAST_TYPE.SUCCESS, title: "Доступ к воркспейсу отключен" });
    } catch (error) {
      setToast({
        type: TOAST_TYPE.ERROR,
        title: "Не удалось отключить участника",
        message: getErrorMessage(error, "Пользователь может быть единственным администратором."),
      });
    } finally {
      setMutatingMemberId(null);
    }
  };

  return (
    <WorkspaceModalShell {...props} title="Участники воркспейса" icon={<UsersRound className="h-5 w-5" />}>
      {isLoading ? (
        <div className="flex min-h-40 items-center justify-center text-secondary">
          <Loader className="h-5 w-5 animate-spin" />
        </div>
      ) : (
        <div className="overflow-hidden rounded-[1.35rem] bg-layer-1">
          <div className="grid grid-cols-[minmax(14rem,1.7fr)_11rem_8rem_9rem_7rem] gap-3 border-b border-subtle px-4 py-3 text-11 font-semibold uppercase text-tertiary">
            <div>Пользователь</div>
            <div>Роль</div>
            <div>Проекты</div>
            <div>Админ проектов</div>
            <div className="text-right">Доступ</div>
          </div>
          <div className="divide-y divide-subtle">
            {(data ?? []).map((workspaceMember) => (
              <div
                key={workspaceMember.id}
                className="grid grid-cols-[minmax(14rem,1.7fr)_11rem_8rem_9rem_7rem] items-center gap-3 px-4 py-3 text-13"
              >
                <div className="min-w-0">
                  <div className="truncate font-medium text-primary">{getMemberName(workspaceMember)}</div>
                  <div className="truncate text-12 text-tertiary">{workspaceMember.member.email}</div>
                </div>
                <select
                  className="nodedc-settings-select h-9 min-h-9 w-full px-3 text-12"
                  value={workspaceMember.role}
                  disabled={mutatingMemberId === workspaceMember.id}
                  onChange={(event) => handleRoleChange(workspaceMember.id, Number(event.target.value))}
                  aria-label={`Роль: ${getMemberName(workspaceMember)}`}
                >
                  {ROLE_OPTIONS.map((role) => (
                    <option key={role.value} value={role.value}>
                      {role.label}
                    </option>
                  ))}
                </select>
                <div className="text-secondary">{workspaceMember.active_project_count}</div>
                <div className="text-secondary">{workspaceMember.admin_project_count}</div>
                <div className="flex justify-end">
                  <button
                    type="button"
                    onClick={() => handleRemove(workspaceMember)}
                    disabled={mutatingMemberId === workspaceMember.id}
                    className="nodedc-settings-secondary-button flex h-9 min-h-9 items-center gap-2 px-3 text-12"
                    aria-label={`Отключить доступ: ${getMemberName(workspaceMember)}`}
                  >
                    {mutatingMemberId === workspaceMember.id ? (
                      <Loader className="h-3.5 w-3.5 animate-spin" />
                    ) : (
                      <Trash2 className="h-3.5 w-3.5" />
                    )}
                    Отключить
                  </button>
                </div>
              </div>
            ))}
          </div>
          {(data ?? []).length === 0 && <div className="px-4 py-8 text-center text-13 text-tertiary">Активных участников нет</div>}
        </div>
      )}
    </WorkspaceModalShell>
  );
}

export function WorkspaceFeaturesModal(props: TWorkspaceAdminModalProps) {
  const { isOpen, workspaceId } = props;
  const [mutatingFeatureKey, setMutatingFeatureKey] = useState<string | null>(null);
  const { data, isLoading, mutate } = useSWR(
    isOpen && workspaceId ? ["INSTANCE_WORKSPACE_FEATURES", workspaceId] : null,
    () => instanceWorkspaceService.retrieveFeatures(workspaceId as string)
  );

  const handleToggle = async (feature: TInstanceWorkspaceFeature) => {
    if (!workspaceId) return;
    setMutatingFeatureKey(feature.key);
    try {
      await instanceWorkspaceService.updateFeature(workspaceId, feature.key, !feature.is_enabled);
      await mutate();
      setToast({
        type: TOAST_TYPE.SUCCESS,
        title: feature.is_enabled ? "Фича отключена для воркспейса" : "Фича выдана воркспейсу",
      });
    } catch (error) {
      setToast({
        type: TOAST_TYPE.ERROR,
        title: "Не удалось обновить доступ",
        message: getErrorMessage(error, "Попробуйте еще раз."),
      });
    } finally {
      setMutatingFeatureKey(null);
    }
  };

  return (
    <WorkspaceModalShell {...props} title="Функции воркспейса" icon={<ShieldCheck className="h-5 w-5" />}>
      {isLoading ? (
        <div className="flex min-h-40 items-center justify-center text-secondary">
          <Loader className="h-5 w-5 animate-spin" />
        </div>
      ) : (
        <div className="space-y-3">
          {(data?.features ?? []).map((feature) => (
            <div key={feature.key} className="nodedc-settings-card flex items-center justify-between gap-5 p-4">
              <div className="min-w-0">
                <div className="flex flex-wrap items-center gap-2">
                  <div className="text-15 font-semibold text-primary">{feature.title}</div>
                  <span
                    className={cn(
                      "rounded-full px-2 py-0.5 text-11 font-medium",
                      feature.is_enabled ? "bg-accent-primary text-on-color" : "bg-layer-2 text-tertiary"
                    )}
                  >
                    {feature.is_enabled ? "Доступ выдан" : "Не выдано"}
                  </span>
                </div>
                <div className="mt-1 text-13 text-secondary">{feature.description}</div>
                <div className="mt-3 flex flex-wrap gap-2 text-11 text-tertiary">
                  <span className="rounded-full bg-layer-2 px-2 py-1">
                    Workspace: {feature.workspace_setting_enabled ? "включено" : "выключено"}
                  </span>
                  <span className="rounded-full bg-layer-2 px-2 py-1">Доступ: {ACCESS_MODE_LABELS[feature.access_mode]}</span>
                  <span className="rounded-full bg-layer-2 px-2 py-1">
                    OpenAI key: {feature.has_workspace_key ? "есть" : "нет"}
                  </span>
                </div>
              </div>
              <div className="shrink-0">
                <ToggleSwitch
                  value={feature.is_enabled}
                  onChange={() => handleToggle(feature)}
                  size="sm"
                  disabled={mutatingFeatureKey === feature.key}
                />
              </div>
            </div>
          ))}
          {(data?.features ?? []).length === 0 && <div className="px-4 py-8 text-center text-13 text-tertiary">Функции не найдены</div>}
        </div>
      )}
      <div className="mt-5 flex justify-end">
        <Button variant="secondary" size="lg" onClick={props.onClose} className="nodedc-settings-secondary-button">
          Закрыть
        </Button>
      </div>
    </WorkspaceModalShell>
  );
}
