import { Chip } from '@mui/material';

// ==============================|| STATUS CHIP ||============================== //
// Soft-filled status chips used across orders, deliveries, riders, invoices.

const MAP = {
  // orders / deliveries
  pending: { color: 'warning', label: 'Pending' },
  created: { color: 'info', label: 'Created' },
  assigned: { color: 'info', label: 'Assigned' },
  accepted: { color: 'info', label: 'Accepted' },
  arrived: { color: 'info', label: 'Arrived' },
  picked: { color: 'primary', label: 'Picked' },
  'in-transit': { color: 'info', label: 'In Transit' },
  active: { color: 'primary', label: 'Active' },
  delivered: { color: 'success', label: 'Delivered' },
  completed: { color: 'success', label: 'Completed' },
  skipped: { color: 'warning', label: 'Skipped' },
  failed: { color: 'error', label: 'Failed' },
  cancelled: { color: 'error', label: 'Cancelled' },
  // clients (doormile_clients)
  newclient: { color: 'info', label: 'New Client' },
  contacted: { color: 'warning', label: 'Contacted' },
  onboarded: { color: 'success', label: 'Onboarded' },
  lost: { color: 'error', label: 'Lost' },
  // team users (doormile_auth)
  admin: { color: 'primary', label: 'Admin' },
  rep: { color: 'info', label: 'Rep' },
  manager: { color: 'success', label: 'Manager' },
  support: { color: 'warning', label: 'Support' },
  // riders / tenants
  online: { color: 'success', label: 'Online' },
  offline: { color: 'default', label: 'Offline' },
  'on-delivery': { color: 'info', label: 'On Delivery' },
  inactive: { color: 'default', label: 'Inactive' },
  // invoices
  paid: { color: 'success', label: 'Paid' },
  open: { color: 'info', label: 'Open' },
  overdue: { color: 'error', label: 'Overdue' },
  prepaid: { color: 'success', label: 'Prepaid' },
  cod: { color: 'warning', label: 'COD' }
};

const TONE = {
  success: { bg: '#E3F6EC', fg: '#00773B' },
  warning: { bg: '#FFF7E0', fg: '#8A6500' },
  info: { bg: '#E0F7F8', fg: '#00727B' },
  error: { bg: '#FEEAE9', fg: '#A82216' },
  primary: { bg: '#F8E0E3', fg: '#9E0E20' },
  default: { bg: '#F0F0F0', fg: '#595959' }
};

export default function StatusChip({ status, size = 'small', label, sx }) {
  const key = String(status || '').toLowerCase().replace(/\s+/g, '-');
  const humanized = String(status || '').replace(/[_-]+/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
  const cfg = MAP[key] || { color: 'default', label: humanized || status };
  const tone = TONE[cfg.color] || TONE.default;
  return (
    <Chip
      size={size}
      label={label || cfg.label}
      sx={{ bgcolor: tone.bg, color: tone.fg, border: 'none', ...sx }}
    />
  );
}
