// Side drawer that lists every upcoming booking grouped by month.
function PipelineDrawer({ onClose, pipeline, houses, totalPaid, totalPending, total }) {
  // Group by month
  const byMonth = {};
  pipeline.forEach(b => {
    const key = b.checkInDate.slice(0, 7);
    (byMonth[key] = byMonth[key] || []).push(b);
  });
  const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December'];

  return (
    <>
      <div onClick={onClose} style={{
        position: 'fixed', inset: 0, background: 'rgba(0,0,0,0.35)', zIndex: 90,
        animation: 'fadeIn 0.15s ease',
      }}/>
      <div style={{
        position: 'fixed', top: 0, right: 0, bottom: 0, width: 560, maxWidth: '90vw',
        background: 'var(--bg-elev)', borderLeft: '1px solid var(--border)',
        zIndex: 91, overflow: 'auto',
        animation: 'slideInRight 0.22s ease',
        boxShadow: '-12px 0 40px rgba(0,0,0,0.12)',
      }}>
        <div style={{ padding: '24px 28px 20px', borderBottom: '1px solid var(--border)', position: 'sticky', top: 0, background: 'var(--bg-elev)', zIndex: 1 }}>
          <div className="row between">
            <div>
              <div className="xs muted" style={{ letterSpacing: 0.5, textTransform: 'uppercase' }}>Pipeline — excluded from revenue</div>
              <div className="serif" style={{ fontSize: 26, fontWeight: 400, letterSpacing: '-0.02em', marginTop: 4 }}>
                {fmt$(total)} <span className="muted" style={{ fontSize: 14 }}>in {pipeline.length} upcoming bookings</span>
              </div>
            </div>
            <button onClick={onClose} className="btn ghost" style={{ width: 32, height: 32, padding: 0 }}>×</button>
          </div>
          <div className="row" style={{ gap: 20, marginTop: 14 }}>
            <div>
              <div className="xs muted">Paid in advance</div>
              <div className="tabular" style={{ fontSize: 15, fontWeight: 500, marginTop: 2 }}>{fmt$(totalPaid)}</div>
            </div>
            <div>
              <div className="xs muted">Pending payment</div>
              <div className="tabular" style={{ fontSize: 15, fontWeight: 500, marginTop: 2, color: 'var(--expense)' }}>{fmt$(totalPending)}</div>
            </div>
          </div>
          <div className="small muted" style={{ marginTop: 12, lineHeight: 1.5, padding: '10px 12px', background: 'var(--bg-sunken)', borderRadius: 6 }}>
            These become revenue automatically the day each guest checks in — provided payment has cleared by then.
          </div>
        </div>

        <div style={{ padding: '16px 28px 40px' }}>
          {Object.keys(byMonth).sort().map(key => {
            const [y, m] = key.split('-');
            const monthTotal = byMonth[key].reduce((s, b) => s + b.amount, 0);
            return (
              <div key={key} style={{ marginTop: 20 }}>
                <div className="row between" style={{ marginBottom: 8 }}>
                  <div style={{ fontWeight: 600, fontSize: 13 }}>{monthNames[Number(m) - 1]} {y}</div>
                  <div className="xs muted tabular">{byMonth[key].length} bookings · {fmt$(monthTotal, { compact: true })}</div>
                </div>
                <div style={{ display: 'flex', flexDirection: 'column', gap: 1, background: 'var(--border)', border: '1px solid var(--border)', borderRadius: 8, overflow: 'hidden' }}>
                  {byMonth[key].map(b => {
                    const house = houses.find(h => h.id === b.houseId);
                    return (
                      <div key={b.id} className="row between" style={{ padding: '12px 14px', background: 'var(--bg-elev)', gap: 12 }}>
                        <div style={{ minWidth: 0, flex: 1 }}>
                          <div className="row" style={{ gap: 8 }}>
                            <span className="dot" style={{ background: house?.accentColor, width: 7, height: 7 }}/>
                            <div style={{ fontWeight: 500, fontSize: 13, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
                              {b.description}
                            </div>
                          </div>
                          <div className="xs muted" style={{ marginTop: 3 }}>
                            {house?.name} · {fmtDate(b.checkInDate)} → {fmtDate(b.checkOutDate)} · {b.channel}
                          </div>
                        </div>
                        <div style={{ textAlign: 'right', flexShrink: 0 }}>
                          <div className="tabular" style={{ fontSize: 13, fontWeight: 500 }}>{fmt$(b.amount)}</div>
                          <div className="xs" style={{ marginTop: 2, color: b.paymentStatus === 'Paid' ? 'var(--income)' : 'var(--expense)' }}>
                            {b.paymentStatus === 'Paid' ? '✓ Paid' : '○ Pending'}
                          </div>
                        </div>
                      </div>
                    );
                  })}
                </div>
              </div>
            );
          })}
        </div>
      </div>
    </>
  );
}

Object.assign(window, { PipelineDrawer });
