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72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
Copyright (C) 2025 QuantumNous

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

For commercial licensing, please contact support@quantumnous.com
*/

import React from 'react';
import { useTranslation } from 'react-i18next';
import {
  API,
  showError,
  showSuccess,
  getOAuthProviderIcon,
} from '../../../../helpers';
import {
  Modal,
  Spin,
  Typography,
  Card,
  Checkbox,
  Tag,
  Button,
} from '@douyinfe/semi-ui';
import {
  IconLink,
  IconMail,
  IconDelete,
  IconGithubLogo,
} from '@douyinfe/semi-icons';
import { SiDiscord, SiTelegram, SiWechat, SiLinux } from 'react-icons/si';

const { Text } = Typography;

const UserBindingManagementModal = ({
  visible,
  onCancel,
  userId,
  isMobile,
  formApiRef,
}) => {
  const { t } = useTranslation();
  const [bindingLoading, setBindingLoading] = React.useState(false);
  const [showBoundOnly, setShowBoundOnly] = React.useState(true);
  const [statusInfo, setStatusInfo] = React.useState({});
  const [customOAuthBindings, setCustomOAuthBindings] = React.useState([]);
  const [builtInBindings, setBuiltInBindings] = React.useState({});
  const [bindingActionLoading, setBindingActionLoading] = React.useState({});

  const loadBindingData = React.useCallback(async () => {
    if (!userId) return;

    setBindingLoading(true);
    try {
      const [statusRes, customBindingRes, userRes] = await Promise.all([
        API.get('/api/status'),
        API.get(`/api/user/${userId}/oauth/bindings`),
        API.get(`/api/user/${userId}`),
      ]);

      if (statusRes.data?.success) {
        setStatusInfo(statusRes.data.data || {});
      } else {
        showError(statusRes.data?.message || t('操作失败'));
      }

      if (customBindingRes.data?.success) {
        setCustomOAuthBindings(customBindingRes.data.data || []);
      } else {
        showError(customBindingRes.data?.message || t('操作失败'));
      }

      if (userRes.data?.success) {
        const userData = userRes.data.data || {};
        setBuiltInBindings({
          email: userData.email || '',
          github_id: userData.github_id || '',
          discord_id: userData.discord_id || '',
          oidc_id: userData.oidc_id || '',
          wechat_id: userData.wechat_id || '',
          telegram_id: userData.telegram_id || '',
          linux_do_id: userData.linux_do_id || '',
        });
      } else {
        showError(userRes.data?.message || t('操作失败'));
      }
    } catch (error) {
      showError(
        error.response?.data?.message || error.message || t('操作失败'),
      );
    } finally {
      setBindingLoading(false);
    }
  }, [t, userId]);

  React.useEffect(() => {
    if (!visible) return;
    setShowBoundOnly(true);
    setBindingActionLoading({});
    loadBindingData();
  }, [visible, loadBindingData]);

  const setBindingLoadingState = (key, value) => {
    setBindingActionLoading((prev) => ({ ...prev, [key]: value }));
  };

  const handleUnbindBuiltInAccount = (bindingItem) => {
    if (!userId) return;

    Modal.confirm({
      title: t('确认解绑'),
      content: t('确定要解绑 {{name}} 吗?', { name: bindingItem.name }),
      okText: t('确认'),
      cancelText: t('取消'),
      onOk: async () => {
        const loadingKey = `builtin-${bindingItem.key}`;
        setBindingLoadingState(loadingKey, true);
        try {
          const res = await API.delete(
            `/api/user/${userId}/bindings/${bindingItem.key}`,
          );
          if (!res.data?.success) {
            showError(res.data?.message || t('操作失败'));
            return;
          }
          setBuiltInBindings((prev) => ({
            ...prev,
            [bindingItem.field]: '',
          }));
          formApiRef.current?.setValue(bindingItem.field, '');
          showSuccess(t('解绑成功'));
        } catch (error) {
          showError(
            error.response?.data?.message || error.message || t('操作失败'),
          );
        } finally {
          setBindingLoadingState(loadingKey, false);
        }
      },
    });
  };

  const handleUnbindCustomOAuthAccount = (provider) => {
    if (!userId) return;

    Modal.confirm({
      title: t('确认解绑'),
      content: t('确定要解绑 {{name}} 吗?', { name: provider.name }),
      okText: t('确认'),
      cancelText: t('取消'),
      onOk: async () => {
        const loadingKey = `custom-${provider.id}`;
        setBindingLoadingState(loadingKey, true);
        try {
          const res = await API.delete(
            `/api/user/${userId}/oauth/bindings/${provider.id}`,
          );
          if (!res.data?.success) {
            showError(res.data?.message || t('操作失败'));
            return;
          }
          setCustomOAuthBindings((prev) =>
            prev.filter(
              (item) => Number(item.provider_id) !== Number(provider.id),
            ),
          );
          showSuccess(t('解绑成功'));
        } catch (error) {
          showError(
            error.response?.data?.message || error.message || t('操作失败'),
          );
        } finally {
          setBindingLoadingState(loadingKey, false);
        }
      },
    });
  };

  const currentValues = formApiRef.current?.getValues?.() || {};
  const getBuiltInBindingValue = (field) =>
    builtInBindings[field] || currentValues[field] || '';

  const builtInBindingItems = [
    {
      key: 'email',
      field: 'email',
      name: t('邮箱'),
      enabled: true,
      value: getBuiltInBindingValue('email'),
      icon: (
        <IconMail
          size='default'
          className='text-slate-600 dark:text-slate-300'
        />
      ),
    },
    {
      key: 'github',
      field: 'github_id',
      name: 'GitHub',
      enabled: Boolean(statusInfo.github_oauth),
      value: getBuiltInBindingValue('github_id'),
      icon: (
        <IconGithubLogo
          size='default'
          className='text-slate-600 dark:text-slate-300'
        />
      ),
    },
    {
      key: 'discord',
      field: 'discord_id',
      name: 'Discord',
      enabled: Boolean(statusInfo.discord_oauth),
      value: getBuiltInBindingValue('discord_id'),
      icon: (
        <SiDiscord size={20} className='text-slate-600 dark:text-slate-300' />
      ),
    },
    {
      key: 'oidc',
      field: 'oidc_id',
      name: 'OIDC',
      enabled: Boolean(statusInfo.oidc_enabled),
      value: getBuiltInBindingValue('oidc_id'),
      icon: (
        <IconLink
          size='default'
          className='text-slate-600 dark:text-slate-300'
        />
      ),
    },
    {
      key: 'wechat',
      field: 'wechat_id',
      name: t('微信'),
      enabled: Boolean(statusInfo.wechat_login),
      value: getBuiltInBindingValue('wechat_id'),
      icon: (
        <SiWechat size={20} className='text-slate-600 dark:text-slate-300' />
      ),
    },
    {
      key: 'telegram',
      field: 'telegram_id',
      name: 'Telegram',
      enabled: Boolean(statusInfo.telegram_oauth),
      value: getBuiltInBindingValue('telegram_id'),
      icon: (
        <SiTelegram size={20} className='text-slate-600 dark:text-slate-300' />
      ),
    },
    {
      key: 'linuxdo',
      field: 'linux_do_id',
      name: 'LinuxDO',
      enabled: Boolean(statusInfo.linuxdo_oauth),
      value: getBuiltInBindingValue('linux_do_id'),
      icon: (
        <SiLinux size={20} className='text-slate-600 dark:text-slate-300' />
      ),
    },
  ];

  const customBindingMap = new Map(
    customOAuthBindings.map((item) => [Number(item.provider_id), item]),
  );

  const customProviderMap = new Map(
    (statusInfo.custom_oauth_providers || []).map((provider) => [
      Number(provider.id),
      provider,
    ]),
  );

  customOAuthBindings.forEach((binding) => {
    if (!customProviderMap.has(Number(binding.provider_id))) {
      customProviderMap.set(Number(binding.provider_id), {
        id: binding.provider_id,
        name: binding.provider_name,
        icon: binding.provider_icon,
      });
    }
  });

  const customBindingItems = Array.from(customProviderMap.values()).map(
    (provider) => {
      const binding = customBindingMap.get(Number(provider.id));
      return {
        key: `custom-${provider.id}`,
        providerId: provider.id,
        name: provider.name,
        enabled: true,
        value: binding?.provider_user_id || '',
        icon: getOAuthProviderIcon(
          provider.icon || binding?.provider_icon || '',
          20,
        ),
      };
    },
  );

  const allBindingItems = [
    ...builtInBindingItems.map((item) => ({ ...item, type: 'builtin' })),
    ...customBindingItems.map((item) => ({ ...item, type: 'custom' })),
  ];

  const boundCount = allBindingItems.filter((item) =>
    Boolean(item.value),
  ).length;

  const visibleBindingItems = showBoundOnly
    ? allBindingItems.filter((item) => Boolean(item.value))
    : allBindingItems;

  return (
    <Modal
      centered
      visible={visible}
      onCancel={onCancel}
      footer={null}
      width={isMobile ? '100%' : 760}
      title={
        <div className='flex items-center'>
          <IconLink className='mr-2' />
          {t('账户绑定管理')}
        </div>
      }
    >
      <Spin spinning={bindingLoading}>
        <div className='max-h-[68vh] overflow-y-auto pr-1 pb-2'>
          <div className='flex items-center justify-between mb-4 gap-3 flex-wrap'>
            <Checkbox
              checked={showBoundOnly}
              onChange={(e) => setShowBoundOnly(Boolean(e.target.checked))}
            >
              {t('仅显示已绑定')}
            </Checkbox>
            <Text type='tertiary'>
              {t('已绑定')} {boundCount} / {allBindingItems.length}
            </Text>
          </div>

          {visibleBindingItems.length === 0 ? (
            <Card className='!rounded-xl border-dashed'>
              <Text type='tertiary'>{t('暂无已绑定项')}</Text>
            </Card>
          ) : (
            <div className='grid grid-cols-1 lg:grid-cols-2 gap-4'>
              {visibleBindingItems.map((item, index) => {
                const isBound = Boolean(item.value);
                const loadingKey =
                  item.type === 'builtin'
                    ? `builtin-${item.key}`
                    : `custom-${item.providerId}`;
                const statusText = isBound
                  ? item.value
                  : item.enabled
                    ? t('未绑定')
                    : t('未启用');
                const shouldSpanTwoColsOnDesktop =
                  visibleBindingItems.length % 2 === 1 &&
                  index === visibleBindingItems.length - 1;

                return (
                  <Card
                    key={item.key}
                    className={`!rounded-xl ${shouldSpanTwoColsOnDesktop ? 'lg:col-span-2' : ''}`}
                  >
                    <div className='flex items-center justify-between gap-3 min-h-[92px]'>
                      <div className='flex items-center flex-1 min-w-0'>
                        <div className='w-10 h-10 rounded-full bg-slate-100 dark:bg-slate-700 flex items-center justify-center mr-3 flex-shrink-0'>
                          {item.icon}
                        </div>
                        <div className='min-w-0 flex-1'>
                          <div className='font-medium text-gray-900 flex items-center gap-2'>
                            <span>{item.name}</span>
                            <Tag size='small' color='white'>
                              {item.type === 'builtin'
                                ? t('内置')
                                : t('自定义')}
                            </Tag>
                          </div>
                          <div className='text-sm text-gray-500 truncate'>
                            {statusText}
                          </div>
                        </div>
                      </div>
                      <Button
                        type='danger'
                        theme='borderless'
                        icon={<IconDelete />}
                        size='small'
                        disabled={!isBound}
                        loading={Boolean(bindingActionLoading[loadingKey])}
                        onClick={() => {
                          if (item.type === 'builtin') {
                            handleUnbindBuiltInAccount(item);
                            return;
                          }
                          handleUnbindCustomOAuthAccount({
                            id: item.providerId,
                            name: item.name,
                          });
                        }}
                      >
                        {t('解绑')}
                      </Button>
                    </div>
                  </Card>
                );
              })}
            </div>
          )}
        </div>
      </Spin>
    </Modal>
  );
};

export default UserBindingManagementModal;