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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
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, { useEffect, useRef, useState } from 'react';
import {
  Modal,
  Form,
  InputNumber,
  Typography,
  Card,
  Space,
  Divider,
  Button,
  Tag,
  Banner,
  Spin,
} from '@douyinfe/semi-ui';
import {
  FaClock,
  FaCalculator,
  FaInfoCircle,
  FaExclamationTriangle,
} from 'react-icons/fa';
import { API, showError, showSuccess } from '../../../../helpers';

const { Text } = Typography;

const ExtendDurationModal = ({
  visible,
  onCancel,
  deployment,
  onSuccess,
  t,
}) => {
  const formRef = useRef(null);
  const [loading, setLoading] = useState(false);
  const [durationHours, setDurationHours] = useState(1);
  const [costLoading, setCostLoading] = useState(false);
  const [priceEstimation, setPriceEstimation] = useState(null);
  const [priceError, setPriceError] = useState(null);
  const [detailsLoading, setDetailsLoading] = useState(false);
  const [deploymentDetails, setDeploymentDetails] = useState(null);
  const costRequestIdRef = useRef(0);

  const resetState = () => {
    costRequestIdRef.current += 1;
    setDurationHours(1);
    setPriceEstimation(null);
    setPriceError(null);
    setDeploymentDetails(null);
    setCostLoading(false);
  };

  const fetchDeploymentDetails = async (deploymentId) => {
    setDetailsLoading(true);
    try {
      const response = await API.get(`/api/deployments/${deploymentId}`);
      if (response.data.success) {
        const details = response.data.data;
        setDeploymentDetails(details);
        setPriceError(null);
        return details;
      }

      const message = response.data.message || '';
      const errorMessage = t('获取详情失败') + (message ? `: ${message}` : '');
      showError(errorMessage);
      setDeploymentDetails(null);
      setPriceEstimation(null);
      setPriceError(errorMessage);
      return null;
    } catch (error) {
      const message = error?.response?.data?.message || error.message || '';
      const errorMessage = t('获取详情失败') + (message ? `: ${message}` : '');
      showError(errorMessage);
      setDeploymentDetails(null);
      setPriceEstimation(null);
      setPriceError(errorMessage);
      return null;
    } finally {
      setDetailsLoading(false);
    }
  };

  const calculatePrice = async (hours, details) => {
    if (!visible || !details) {
      return;
    }

    const sanitizedHours = Number.isFinite(hours) ? Math.round(hours) : 0;
    if (sanitizedHours <= 0) {
      setPriceEstimation(null);
      setPriceError(null);
      return;
    }

    const hardwareId = Number(details?.hardware_id) || 0;
    const totalGPUs = Number(details?.total_gpus) || 0;
    const totalContainers = Number(details?.total_containers) || 0;
    const baseGpusPerContainer = Number(details?.gpus_per_container) || 0;
    const resolvedGpusPerContainer =
      baseGpusPerContainer > 0
        ? baseGpusPerContainer
        : totalContainers > 0 && totalGPUs > 0
          ? Math.max(1, Math.round(totalGPUs / totalContainers))
          : 0;
    const resolvedReplicaCount =
      totalContainers > 0
        ? totalContainers
        : resolvedGpusPerContainer > 0 && totalGPUs > 0
          ? Math.max(1, Math.round(totalGPUs / resolvedGpusPerContainer))
          : 0;
    const locationIds = Array.isArray(details?.locations)
      ? details.locations
          .map((location) =>
            Number(
              location?.id ??
                location?.location_id ??
                location?.locationId,
            ),
          )
          .filter((id) => Number.isInteger(id) && id > 0)
      : [];

    if (
      hardwareId <= 0 ||
      resolvedGpusPerContainer <= 0 ||
      resolvedReplicaCount <= 0 ||
      locationIds.length === 0
    ) {
      setPriceEstimation(null);
      setPriceError(t('价格计算失败'));
      return;
    }

    const requestId = Date.now();
    costRequestIdRef.current = requestId;
    setCostLoading(true);
    setPriceError(null);

    const payload = {
      location_ids: locationIds,
      hardware_id: hardwareId,
      gpus_per_container: resolvedGpusPerContainer,
      duration_hours: sanitizedHours,
      replica_count: resolvedReplicaCount,
      currency: 'usdc',
      duration_type: 'hour',
      duration_qty: sanitizedHours,
      hardware_qty: resolvedGpusPerContainer,
    };

    try {
      const response = await API.post(
        '/api/deployments/price-estimation',
        payload,
      );

      if (costRequestIdRef.current !== requestId) {
        return;
      }

      if (response.data.success) {
        setPriceEstimation(response.data.data);
      } else {
        const message = response.data.message || '';
        setPriceEstimation(null);
        setPriceError(
          t('价格计算失败') + (message ? `: ${message}` : ''),
        );
      }
    } catch (error) {
      if (costRequestIdRef.current !== requestId) {
        return;
      }

      const message = error?.response?.data?.message || error.message || '';
      setPriceEstimation(null);
      setPriceError(
        t('价格计算失败') + (message ? `: ${message}` : ''),
      );
    } finally {
      if (costRequestIdRef.current === requestId) {
        setCostLoading(false);
      }
    }
  };

  useEffect(() => {
    if (visible && deployment?.id) {
      resetState();
      if (formRef.current) {
        formRef.current.setValue('duration_hours', 1);
      }
      fetchDeploymentDetails(deployment.id);
    }
    if (!visible) {
      resetState();
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [visible, deployment?.id]);

  useEffect(() => {
    if (!visible) {
      return;
    }
    if (!deploymentDetails) {
      return;
    }
    calculatePrice(durationHours, deploymentDetails);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [durationHours, deploymentDetails, visible]);

  const handleExtend = async () => {
    try {
      if (formRef.current) {
        await formRef.current.validate();
      }
      setLoading(true);

      const response = await API.post(
        `/api/deployments/${deployment.id}/extend`,
        {
          duration_hours: Math.round(durationHours),
        },
      );

      if (response.data.success) {
        showSuccess(t('容器时长延长成功'));
        onSuccess?.(response.data.data);
        handleCancel();
      }
    } catch (error) {
      showError(
        t('延长时长失败') +
          ': ' +
          (error?.response?.data?.message || error.message),
      );
    } finally {
      setLoading(false);
    }
  };

  const handleCancel = () => {
    if (formRef.current) {
      formRef.current.reset();
    }
    resetState();
    onCancel();
  };

  const currentRemainingTime = deployment?.time_remaining || '0分钟';
  const newTotalTime = `${currentRemainingTime} + ${durationHours}${t('小时')}`;

  const priceData = priceEstimation || {};
  const breakdown =
    priceData.price_breakdown || priceData.PriceBreakdown || {};
  const currencyLabel = (
    priceData.currency || priceData.Currency || 'USDC'
  )
    .toString()
    .toUpperCase();

  const estimatedTotalCost =
    typeof priceData.estimated_cost === 'number'
      ? priceData.estimated_cost
      : typeof priceData.EstimatedCost === 'number'
        ? priceData.EstimatedCost
        : typeof breakdown.total_cost === 'number'
          ? breakdown.total_cost
          : breakdown.TotalCost;
  const hourlyRate =
    typeof breakdown.hourly_rate === 'number'
      ? breakdown.hourly_rate
      : breakdown.HourlyRate;
  const computeCost =
    typeof breakdown.compute_cost === 'number'
      ? breakdown.compute_cost
      : breakdown.ComputeCost;

  const resolvedHardwareName =
    deploymentDetails?.hardware_name || deployment?.hardware_name || '--';
  const gpuCount =
    deploymentDetails?.total_gpus || deployment?.hardware_quantity || 0;
  const containers = deploymentDetails?.total_containers || 0;

  return (
    <Modal
      title={
        <div className='flex items-center gap-2'>
          <FaClock className='text-blue-500' />
          <span>{t('延长容器时长')}</span>
        </div>
      }
      visible={visible}
      onCancel={handleCancel}
      onOk={handleExtend}
      okText={t('确认延长')}
      cancelText={t('取消')}
      confirmLoading={loading}
      okButtonProps={{
        disabled:
          !deployment?.id || detailsLoading || !durationHours || durationHours < 1,
      }}
      width={600}
      className='extend-duration-modal'
    >
      <div className='space-y-4'>
        <Card className='border-0 bg-gray-50'>
          <div className='flex items-center justify-between'>
            <div>
              <Text strong className='text-base'>
                {deployment?.container_name || deployment?.deployment_name}
              </Text>
              <div className='mt-1'>
                <Text type='secondary' size='small'>
                  ID: {deployment?.id}
                </Text>
              </div>
            </div>
            <div className='text-right'>
              <div className='flex items-center gap-2 mb-1'>
                <Tag color='blue' size='small'>
                  {resolvedHardwareName}
                  {gpuCount ? ` x${gpuCount}` : ''}
                </Tag>
              </div>
              <Text size='small' type='secondary'>
                {t('当前剩余')}: <Text strong>{currentRemainingTime}</Text>
              </Text>
            </div>
          </div>
        </Card>

        <Banner
          type='warning'
          icon={<FaExclamationTriangle />}
          title={t('重要提醒')}
          description={
            <div className='space-y-2'>
              <p>
                {t('延长容器时长将会产生额外费用,请确认您有足够的账户余额。')}
              </p>
              <p>
                {t('延长操作一旦确认无法撤销,费用将立即扣除。')}
              </p>
            </div>
          }
        />

        <Form
          getFormApi={(api) => (formRef.current = api)}
          layout='vertical'
          onValueChange={(values) => {
            if (values.duration_hours !== undefined) {
              const numericValue = Number(values.duration_hours);
              setDurationHours(Number.isFinite(numericValue) ? numericValue : 0);
            }
          }}
        >
          <Form.InputNumber
            field='duration_hours'
            label={t('延长时长小时)')}
            placeholder={t('请输入要延长的小时数')}
            min={1}
            max={720}
            step={1}
            initValue={1}
            style={{ width: '100%' }}
            suffix={t('小时')}
            rules={[
              { required: true, message: t('请输入延长时长') },
              {
                type: 'number',
                min: 1,
                message: t('延长时长至少为1小时'),
              },
              {
                type: 'number',
                max: 720,
                message: t('延长时长不能超过720小时30天)'),
              },
            ]}
          />
        </Form>

        <div className='space-y-2'>
          <Text size='small' type='secondary'>
            {t('快速选择')}:
          </Text>
          <Space wrap>
            {[1, 2, 6, 12, 24, 48, 72, 168].map((hours) => (
              <Button
                key={hours}
                size='small'
                theme={durationHours === hours ? 'solid' : 'borderless'}
                type={durationHours === hours ? 'primary' : 'secondary'}
                onClick={() => {
                  setDurationHours(hours);
                  if (formRef.current) {
                    formRef.current.setValue('duration_hours', hours);
                  }
                }}
              >
                {hours < 24
                  ? `${hours}${t('小时')}`
                  : `${hours / 24}${t('天')}`}
              </Button>
            ))}
          </Space>
        </div>

        <Divider />

        <Card
          title={
            <div className='flex items-center gap-2'>
              <FaCalculator className='text-green-500' />
              <span>{t('费用预估')}</span>
            </div>
          }
          className='border border-green-200'
        >
          {priceEstimation ? (
            <div className='space-y-3'>
              <div className='flex items-center justify-between'>
                <Text>{t('延长时长')}:</Text>
                <Text strong>
                  {Math.round(durationHours)} {t('小时')}
                </Text>
              </div>

              <div className='flex items-center justify-between'>
                <Text>{t('硬件配置')}:</Text>
                <Text strong>
                  {resolvedHardwareName}
                  {gpuCount ? ` x${gpuCount}` : ''}
                </Text>
              </div>

              {containers ? (
                <div className='flex items-center justify-between'>
                  <Text>{t('容器数量')}:</Text>
                  <Text strong>{containers}</Text>
                </div>
              ) : null}

              <div className='flex items-center justify-between'>
                <Text>{t('单GPU小时费率')}:</Text>
                <Text strong>
                  {typeof hourlyRate === 'number'
                    ? `${hourlyRate.toFixed(4)} ${currencyLabel}`
                    : '--'}
                </Text>
              </div>

              {typeof computeCost === 'number' && (
                <div className='flex items-center justify-between'>
                  <Text>{t('计算成本')}:</Text>
                  <Text strong>
                    {computeCost.toFixed(4)} {currencyLabel}
                  </Text>
                </div>
              )}

              <Divider margin='12px' />

              <div className='flex items-center justify-between'>
                <Text strong className='text-lg'>
                  {t('预估总费用')}:
                </Text>
                <Text strong className='text-lg text-green-600'>
                  {typeof estimatedTotalCost === 'number'
                    ? `${estimatedTotalCost.toFixed(4)} ${currencyLabel}`
                    : '--'}
                </Text>
              </div>

              <div className='bg-blue-50 p-3 rounded-lg'>
                <div className='flex items-start gap-2'>
                  <FaInfoCircle className='text-blue-500 mt-0.5' />
                  <div>
                    <Text size='small' type='secondary'>
                      {t('延长后总时长')}: <Text strong>{newTotalTime}</Text>
                    </Text>
                    <br />
                    <Text size='small' type='secondary'>
                      {t('预估费用仅供参考,实际费用可能略有差异')}
                    </Text>
                  </div>
                </div>
              </div>
            </div>
          ) : (
            <div className='text-center text-gray-500 py-4'>
              {costLoading ? (
                <Space align='center' className='justify-center'>
                  <Spin size='small' />
                  <Text type='secondary'>{t('计算费用中...')}</Text>
                </Space>
              ) : priceError ? (
                <Text type='danger'>{priceError}</Text>
              ) : deploymentDetails ? (
                <Text type='secondary'>{t('请输入延长时长')}</Text>
              ) : (
                <Text type='secondary'>{t('加载详情中...')}</Text>
              )}
            </div>
          )}
        </Card>

        <div className='bg-red-50 border border-red-200 rounded-lg p-3'>
          <div className='flex items-start gap-2'>
            <FaExclamationTriangle className='text-red-500 mt-0.5' />
            <div>
              <Text strong className='text-red-700'>
                {t('确认延长容器时长')}
              </Text>
              <div className='mt-1'>
                <Text size='small' className='text-red-600'>
                  {t('点击"确认延长"后将立即扣除费用并延长容器运行时间')}
                </Text>
              </div>
            </div>
          </div>
        </div>
      </div>
    </Modal>
  );
};

export default ExtendDurationModal;