import PropTypes from 'prop-types';
import { Chip } from '@mui/material';

// ==============================|| STATUS CHIP (Doormile-style soft badge) ||============================== //
// One consistent soft-filled chip for every lifecycle status across orders,
// deliveries, riders, tenants, invoices. Colour encodes meaning; the chip stays
// quiet (soft bg + readable dark text) so tables don't turn into a rainbow.

// Soft background + readable foreground per semantic tone.
const TONE = {
  amber:   { bg: '#FEF3C7', fg: '#92400E' },
  indigo:  { bg: '#E0E7FF', fg: '#3730A3' },
  cyan:    { bg: '#CFFAFE', fg: '#155E75' },
  violet:  { bg: '#EDE9FE', fg: '#5B21B6' },
  teal:    { bg: '#CCFBF1', fg: '#115E59' },
  emerald: { bg: '#D1FAE5', fg: '#065F46' },
  red:     { bg: '#FEE2E2', fg: '#991B1B' },
  orange:  { bg: '#FFEDD5', fg: '#9A3412' },
  sky:     { bg: '#E0F2FE', fg: '#075985' },
  slate:   { bg: '#F1F5F9', fg: '#475569' },
  brand:   { bg: '#F1E6F7', fg: '#5B1F73' }
};

// Status keyword -> tone + display label.
const MAP = {
  pending:    { tone: 'amber',   label: 'Pending' },
  created:    { tone: 'sky',     label: 'Created' },
  assigned:   { tone: 'indigo',  label: 'Assigned' },
  accepted:   { tone: 'indigo',  label: 'Accepted' },
  arrived:    { tone: 'cyan',    label: 'Arrived' },
  picked:     { tone: 'violet',  label: 'Picked' },
  'picked-up':{ tone: 'violet',  label: 'Picked Up' },
  started:    { tone: 'cyan',    label: 'Started' },
  active:     { tone: 'teal',    label: 'Active' },
  'in-transit': { tone: 'teal',  label: 'In Transit' },
  delivered:  { tone: 'emerald', label: 'Delivered' },
  completed:  { tone: 'emerald', label: 'Completed' },
  skipped:    { tone: 'orange',  label: 'Skipped' },
  failed:     { tone: 'red',     label: 'Failed' },
  cancelled:  { tone: 'red',     label: 'Cancelled' },
  // riders / tenants
  online:     { tone: 'emerald', label: 'Online' },
  offline:    { tone: 'slate',   label: 'Offline' },
  inactive:   { tone: 'slate',   label: 'Inactive' },
  idle:       { tone: 'amber',   label: 'Idle' },
  unknown:    { tone: 'slate',   label: 'Unknown' },
  // invoices
  paid:       { tone: 'emerald', label: 'Paid' },
  unpaid:     { tone: 'amber',   label: 'Unpaid' },
  open:       { tone: 'sky',     label: 'Open' },
  overdue:    { tone: 'red',     label: 'Overdue' },
  prepaid:    { tone: 'emerald', label: 'Prepaid' },
  cod:        { tone: 'amber',   label: 'COD' }
};

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

StatusChip.propTypes = {
  status: PropTypes.string,
  label: PropTypes.node,
  size: PropTypes.string,
  sx: PropTypes.object
};
