import { useEffect, useState } from 'react';
import Modal from '../../../components/common/Modal/Modal';
import { Box, Button, Typography, Alert } from '@mui/material';
import {
  DangerOutlinedButton,
  SecondaryButton,
} from '../../../components/common/Buttons/Buttons';

export const ModalConfirmArchiveSsp = ({ open, onClose, ssp, onArchive }) => {
  const [loading, setLoading] = useState(false);

  const handleConfirmArchive = async () => {
    if (!ssp) return;
    setLoading(true);
    await onArchive?.(ssp);
    onClose?.();
  };

  useEffect(() => {
    if (!open) {
      setLoading(false);
    }
  }, [open]);

  if (!ssp) return null;

  const sspTypeLabel = ssp.is_ssp ? 'ССП' : 'Региональный филиал';

  return (
    <Modal
      title="Подтверждение"
      open={open}
      onClose={onClose}
      customSize={31.25}
      actions={
        <>
          <SecondaryButton onClick={onClose} disabled={loading}>
            Отмена
          </SecondaryButton>
          <DangerOutlinedButton
            onClick={handleConfirmArchive}
            disabled={loading}
          >
            {loading ? '...' : 'В архив'}
          </DangerOutlinedButton>
        </>
      }
    >
      <Box sx={{ p: 2 }}>
        <Alert severity="warning" sx={{ mb: '.5rem' }}>
          Отправить {sspTypeLabel.toLowerCase()} в архив?
        </Alert>

        <Typography variant="body2" sx={{ mb: 1 }}>
          <strong>{ssp.title || ssp.name || '—'}</strong> · {sspTypeLabel}
        </Typography>

        <Typography variant="caption" color="text.secondary">
          После архивации: скрыт из списка, доступ ограничен.
        </Typography>
      </Box>
    </Modal>
  );
};
