'use client';

import { useEffect, useMemo, useState } from 'react';
import { Chrome, Loader2, Save, Settings2, ToggleLeft, ToggleRight } from 'lucide-react';
import { useAuth } from '@/components/id/auth-provider';
import { AdminShell } from '@/components/id/admin-shell';
import { usePublicSettings } from '@/components/id/public-settings-provider';
import { useToast } from '@/components/id/toast-provider';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { apiFetch, buildSystemSettingPayload, SocialProvider, SystemSetting } from '@/lib/api';
import { getSettingMeta, sortSettingsByCatalog, SYSTEM_SETTING_GROUPS } from '@/lib/system-settings-catalog';

function parseBoolean(value: string) {
  return ['true', '1', 'yes'].includes(value.trim().toLowerCase());
}

export default function AdminSettingsPage() {
  const { token, user } = useAuth();
  const { showToast } = useToast();
  const { refreshPublicSettings } = usePublicSettings();
  const [settings, setSettings] = useState<SystemSetting[]>([]);
  const [providers, setProviders] = useState<SocialProvider[]>([]);
  const [loading, setLoading] = useState(true);
  const [savingKey, setSavingKey] = useState<string | null>(null);

  useEffect(() => {
    if (!token || !user?.canManageSettings) return;
    Promise.all([
      apiFetch<{ settings: SystemSetting[] }>('/admin/settings', {}, token),
      apiFetch<{ providers: SocialProvider[] }>('/admin/settings/oauth/providers', {}, token)
    ])
      .then(([settingsResponse, providersResponse]) => {
        setSettings(sortSettingsByCatalog(settingsResponse.settings ?? []));
        setProviders(providersResponse.providers ?? []);
      })
      .catch((error) => showToast(error instanceof Error ? error.message : 'Не удалось загрузить настройки'))
      .finally(() => setLoading(false));
  }, [showToast, token, user?.canManageSettings]);

  const groupedSettings = useMemo(() => {
    const groups = new Map<string, SystemSetting[]>();
    for (const setting of settings) {
      const meta = getSettingMeta(setting.key);
      const group = meta?.group ?? 'other';
      const list = groups.get(group) ?? [];
      list.push(setting);
      groups.set(group, list);
    }
    return groups;
  }, [settings]);

  async function saveSetting(setting: SystemSetting) {
    if (!token) return;
    setSavingKey(setting.key);
    try {
      const updated = await apiFetch<SystemSetting>(
        '/admin/settings',
        {
          method: 'PUT',
          body: JSON.stringify(buildSystemSettingPayload(setting))
        },
        token
      );
      setSettings((current) => sortSettingsByCatalog(current.map((item) => (item.key === updated.key ? updated : item))));
      if (updated.key === 'PROJECT_NAME' || updated.key === 'PROJECT_TAGLINE' || updated.key === 'LDAP_ENABLED' || updated.key === 'LDAP_USE_LDAPS') {
        await refreshPublicSettings();
      }
      showToast('Настройка сохранена');
    } catch (error) {
      showToast(error instanceof Error ? error.message : 'Не удалось сохранить настройку');
    } finally {
      setSavingKey(null);
    }
  }

  function updateSettingValue(key: string, value: string) {
    setSettings((current) => current.map((item) => (item.key === key ? { ...item, value } : item)));
  }

  function toProviderPayload(provider: SocialProvider) {
    return {
      providerName: provider.providerName,
      clientId: provider.clientId,
      isEnabled: provider.isEnabled
    };
  }

  async function toggleProvider(provider: SocialProvider) {
    if (!token) return;
    try {
      const updated = await apiFetch<SocialProvider>('/admin/settings/oauth/providers', {
        method: 'PUT',
        body: JSON.stringify({ ...toProviderPayload(provider), isEnabled: !provider.isEnabled })
      }, token);
      setProviders((current) => current.map((item) => (item.providerName === updated.providerName ? updated : item)));
      showToast(updated.isEnabled ? 'Провайдер включён' : 'Провайдер отключён');
    } catch (error) {
      showToast(error instanceof Error ? error.message : 'Не удалось обновить провайдера');
    }
  }

  if (!user?.canManageSettings) {
    return (
      <AdminShell active="/admin/settings">
        <p className="text-[#667085]">У вас нет прав на изменение глобальных настроек.</p>
      </AdminShell>
    );
  }

  return (
    <AdminShell active="/admin/settings">
      <div className="mb-6">
        <h2 className="text-2xl font-medium">Глобальные настройки</h2>
        <p className="mt-2 text-[#667085]">Параметры хранятся в SystemSetting и применяются сервисами без релиза frontend.</p>
      </div>

      {loading ? (
        <div className="flex items-center justify-center gap-2 py-16 text-[#667085]">
          <Loader2 className="h-5 w-5 animate-spin" />
          Загрузка...
        </div>
      ) : (
        <>
          {[...groupedSettings.entries()].map(([groupKey, groupSettings]) => (
            <section key={groupKey} className="mb-10">
              <h3 className="mb-4 text-xl font-medium">{SYSTEM_SETTING_GROUPS[groupKey] ?? 'Прочие настройки'}</h3>
              <div className="space-y-3">
                {groupSettings.map((setting) => {
                  const meta = getSettingMeta(setting.key);
                  const label = meta?.label ?? setting.key;
                  const type = meta?.type ?? 'text';

                  return (
                    <div key={setting.key} className="rounded-[24px] bg-[#f4f5f8] p-5">
                      <div className="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
                        <div className="min-w-0 flex-1">
                          <div className="flex items-center gap-2 font-semibold">
                            <Settings2 className="h-4 w-4 shrink-0" />
                            {label}
                          </div>
                          <p className="mt-1 text-sm text-[#667085]">{meta?.hint ?? setting.description ?? setting.key}</p>
                        </div>

                        <div className="flex flex-wrap items-center gap-3">
                          {type === 'boolean' ? (
                            <button
                              type="button"
                              aria-label={label}
                              onClick={() => {
                                updateSettingValue(setting.key, parseBoolean(setting.value) ? 'false' : 'true');
                              }}
                            >
                              {parseBoolean(setting.value) ? <ToggleRight className="h-9 w-9 text-green-600" /> : <ToggleLeft className="h-9 w-9 text-[#a8adbc]" />}
                            </button>
                          ) : (
                            <div className="flex items-center gap-2">
                              <Input
                                type={setting.isSecret ? 'password' : type === 'number' ? 'number' : 'text'}
                                value={setting.value}
                                className={setting.isSecret ? 'w-[220px] bg-white' : 'w-[180px] bg-white'}
                                onChange={(event) => updateSettingValue(setting.key, event.target.value)}
                              />
                              {meta?.unit ? <span className="text-sm text-[#667085]">{meta.unit}</span> : null}
                            </div>
                          )}

                          <Button disabled={savingKey === setting.key} onClick={() => void saveSetting(setting)}>
                            {savingKey === setting.key ? <Loader2 className="h-4 w-4 animate-spin" /> : <Save className="h-4 w-4" />}
                            Сохранить
                          </Button>
                        </div>
                      </div>
                    </div>
                  );
                })}
              </div>
            </section>
          ))}

          <section className="mt-10">
            <h3 className="text-xl font-medium">OAuth Social Providers</h3>
            <div className="mt-4 grid gap-4 md:grid-cols-2">
              {(providers.length ? providers : [{ id: 'google', providerName: 'google', clientId: '', isEnabled: false }, { id: 'yandex', providerName: 'yandex', clientId: '', isEnabled: false }]).map((provider) => (
                <div key={provider.providerName} className="rounded-[24px] bg-[#f4f5f8] p-5">
                  <div className="flex items-center justify-between">
                    {provider.providerName === 'google' ? <Chrome className="h-7 w-7" /> : <div className="flex h-8 w-8 items-center justify-center rounded-full bg-red-500 font-bold text-white">Я</div>}
                    <button type="button" aria-label="Переключить провайдера" onClick={() => void toggleProvider(provider)}>
                      {provider.isEnabled ? <ToggleRight className="h-8 w-8 text-green-600" /> : <ToggleLeft className="h-8 w-8 text-[#a8adbc]" />}
                    </button>
                  </div>
                  <h4 className="mt-5 font-semibold capitalize">{provider.providerName}</h4>
                  <p className="mt-1 text-sm text-[#667085]">{provider.isEnabled ? 'Провайдер включен глобально.' : 'Провайдер отключен.'}</p>
                  <Input
                    className="mt-4 bg-white"
                    value={provider.clientId}
                    placeholder="Client ID"
                    onChange={(event) => setProviders((current) => current.map((item) => (item.providerName === provider.providerName ? { ...item, clientId: event.target.value } : item)))}
                  />
                  <Button
                    className="mt-3"
                    variant="secondary"
                    onClick={() =>
                      void apiFetch('/admin/settings/oauth/providers', { method: 'PUT', body: JSON.stringify(toProviderPayload(provider)) }, token)
                        .then(() => showToast('Провайдер сохранён'))
                        .catch((error) => showToast(error instanceof Error ? error.message : 'Ошибка сохранения'))
                    }
                  >
                    Сохранить провайдера
                  </Button>
                </div>
              ))}
            </div>
          </section>
        </>
      )}
    </AdminShell>
  );
}
