import { useState, useEffect } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, FormGroup, FormControlLabel, Checkbox, Typography, Alert, Box, } from '@mui/material'; import type { SubscriptionTier } from '../types/subscription.types'; import { getVehicleLabel } from '@/core/utils/vehicleDisplay'; interface Vehicle { id: string; make?: string; model?: string; year?: number; nickname?: string; } interface VehicleSelectionDialogProps { open: boolean; onClose: () => void; onConfirm: (selectedVehicleIds: string[]) => void; vehicles: Vehicle[]; maxSelections: number; targetTier: SubscriptionTier; /** When true, dialog cannot be dismissed - user must make a selection */ blocking?: boolean; } export const VehicleSelectionDialog = ({ open, onClose, onConfirm, vehicles, maxSelections, targetTier, blocking = false, }: VehicleSelectionDialogProps) => { const [selectedVehicleIds, setSelectedVehicleIds] = useState([]); // Pre-select first N vehicles when dialog opens useEffect(() => { if (open && vehicles.length > 0) { const initialSelection = vehicles.slice(0, maxSelections).map((v) => v.id); setSelectedVehicleIds(initialSelection); } }, [open, vehicles, maxSelections]); const handleToggle = (vehicleId: string) => { setSelectedVehicleIds((prev) => { if (prev.includes(vehicleId)) { return prev.filter((id) => id !== vehicleId); } else { // Only add if under the limit if (prev.length < maxSelections) { return [...prev, vehicleId]; } return prev; } }); }; const handleConfirm = () => { onConfirm(selectedVehicleIds); }; const canConfirm = selectedVehicleIds.length > 0 && selectedVehicleIds.length <= maxSelections; // Handle dialog close - prevent if blocking const handleClose = (_event: object, reason: string) => { if (blocking && (reason === 'backdropClick' || reason === 'escapeKeyDown')) { return; } onClose(); }; return ( {blocking ? 'Vehicle Selection Required' : 'Select Vehicles to Keep'} {blocking ? ( <> Your subscription has been downgraded to the {targetTier} tier, which allows{' '} {maxSelections} vehicle{maxSelections > 1 ? 's' : ''}. Please select which vehicles you want to keep active to continue using the app. Unselected vehicles will be hidden but not deleted, and you can unlock them by upgrading later. ) : ( <> You are downgrading to the {targetTier} tier, which allows {maxSelections} vehicle {maxSelections > 1 ? 's' : ''}. Select which vehicles you want to keep active. Unselected vehicles will be hidden but not deleted, and you can unlock them by upgrading later. )} Selected {selectedVehicleIds.length} of {maxSelections} allowed {vehicles.map((vehicle) => ( handleToggle(vehicle.id)} disabled={ !selectedVehicleIds.includes(vehicle.id) && selectedVehicleIds.length >= maxSelections } /> } label={getVehicleLabel(vehicle)} /> ))} {selectedVehicleIds.length === 0 && ( You must select at least one vehicle. )} {selectedVehicleIds.length > maxSelections && ( You can only select up to {maxSelections} vehicle{maxSelections > 1 ? 's' : ''}. )} {!blocking && } ); };