/** * @ai-summary Context-aware document delete confirmation dialog * Shows different messages based on whether document is being removed from vehicle or fully deleted */ import React from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, Typography, Box, useMediaQuery, useTheme, } from '@mui/material'; import WarningAmberIcon from '@mui/icons-material/WarningAmber'; import type { DocumentRecord } from '../types/documents.types'; export interface DeleteDocumentConfirmDialogProps { open: boolean; onClose: () => void; onConfirm: (fullDelete: boolean) => void; document: DocumentRecord | null; vehicleId: string | null; } export const DeleteDocumentConfirmDialog: React.FC = ({ open, onClose, onConfirm, document, vehicleId, }) => { const theme = useTheme(); const isSmallScreen = useMediaQuery(theme.breakpoints.down('sm')); if (!document || !vehicleId) { return null; } // Determine delete context const isPrimaryVehicle = document.vehicleId === vehicleId; const isSharedVehicle = document.sharedVehicleIds.includes(vehicleId); const sharedCount = document.sharedVehicleIds.length; let title: string; let message: string; let fullDelete: boolean; let actionText: string; if (isPrimaryVehicle && sharedCount === 0) { // Primary vehicle with no shares: Full delete title = 'Delete Document?'; message = 'This will permanently delete this document. This action cannot be undone.'; fullDelete = true; actionText = 'Delete'; } else if (isSharedVehicle) { // Shared vehicle: Remove association only title = 'Remove Document from Vehicle?'; message = `This will remove the document from this vehicle. The document will remain shared with ${sharedCount - 1 === 1 ? '1 other vehicle' : `${sharedCount - 1} other vehicles`}.`; fullDelete = false; actionText = 'Remove'; } else if (isPrimaryVehicle && sharedCount > 0) { // Primary vehicle with shares: Full delete (affects all) title = 'Delete Document?'; message = `This document is shared with ${sharedCount === 1 ? '1 other vehicle' : `${sharedCount} other vehicles`}. Deleting it will remove it from all vehicles. This action cannot be undone.`; fullDelete = true; actionText = 'Delete'; } else { // Fallback case (should not happen) title = 'Delete Document?'; message = 'This will delete this document. This action cannot be undone.'; fullDelete = true; actionText = 'Delete'; } const handleConfirm = () => { onConfirm(fullDelete); }; return ( {title} {message} {document.title} {document.documentType.charAt(0).toUpperCase() + document.documentType.slice(1)} {document.expirationDate && ` • Expires: ${new Date(document.expirationDate).toLocaleDateString()}`} ); };