// js/components/widgets/quotes/QuoteStatusActions.jsx
// [v224.126 NUCLEAR] Badge de status + botões promover/cancelar (chama promote_quote_status_v1).
// Deps runtime: React, window.sb, window.toast, window.Sentry, window.znx* (lib/quotes/status.js)
(function(){
  'use strict';
  function QuoteStatusActions(props){
    var quote = props.quote, user = props.user, onPromoted = props.onPromoted;
    var _s = React.useState(false); var submitting = _s[0], setSubmitting = _s[1];
    var toast = (typeof window.toast === 'function') ? window.toast : function(m){ console.log('[toast]', m); };

    if(!quote || typeof window.znxQuoteStatusBadge !== 'function') return null;
    var badge = window.znxQuoteStatusBadge(quote.status);
    var horasRest = window.znxHorasRestantes(quote);
    var podePromover = window.znxPodePromover(quote, user);
    var proximoStatus = window.znxProximoStatus(quote.status);

    function handlePromote(newStatus){
      if(submitting) return;
      if(!window.sb || typeof window.sb.rpc !== 'function'){ toast('❌ Conexão indisponível'); return; }
      setSubmitting(true);
      var idem = (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') ? crypto.randomUUID() : null;
      window.sb.rpc('promote_quote_status_v1', { p_quote_id: quote.id, p_new_status: newStatus, p_idem_key: idem })
        .then(function(res){
          if(res && res.error) throw res.error;
          var data = res && res.data;
          var rest = (data && data.horas_restantes_ate_teto != null) ? data.horas_restantes_ate_teto : '';
          toast('✅ ORC promovido pra ' + newStatus + (rest!=='' ? (' · ' + rest + 'h até o teto') : ''));
          if(onPromoted) onPromoted(data || { new_status: newStatus });
        })
        .catch(function(e){
          toast('❌ ' + ((e && e.message) || 'falhou'));
          if(window.Sentry){ try{ window.Sentry.captureException(e, { tags:{ wave:'v224.126' }, extra:{ context:'promote_quote_status', quote_id: quote.id, newStatus: newStatus } }); }catch(_){} }
        })
        .then(function(){ setSubmitting(false); });
    }

    return (
      <div style={{display:'flex',alignItems:'center',gap:8,flexWrap:'wrap'}}>
        <span style={{padding:'4px 10px',borderRadius:12,fontSize:12,fontWeight:700,background:badge.bg,color:badge.fg}}>
          {badge.icon} {badge.label}
        </span>
        {horasRest !== null && (
          <span style={{fontSize:11,fontWeight:700,color: horasRest < 1 ? '#DC2626' : horasRest < 3 ? '#D97706' : '#6B7280'}}>
            ⏱ {horasRest < 1 ? Math.round(horasRest * 60) + 'min' : horasRest.toFixed(1) + 'h'} pra expirar
          </span>
        )}
        {podePromover && proximoStatus && (
          <button onClick={function(){ handlePromote(proximoStatus); }} disabled={submitting}
            style={{padding:'6px 12px',background:'#1B2A4A',color:'#fff',borderRadius:6,fontSize:12,fontWeight:700,border:'none',cursor: submitting ? 'wait' : 'pointer',opacity: submitting ? 0.6 : 1}}>
            {submitting ? '...' : '→ ' + proximoStatus}
          </button>
        )}
        {podePromover && (user.role === 'admin' || user.role === 'financeiro') && (
          <button onClick={function(){ handlePromote('Cancelado'); }} disabled={submitting}
            style={{padding:'6px 12px',background:'#fff',color:'#DC2626',border:'1px solid #DC2626',borderRadius:6,fontSize:12,fontWeight:700,cursor: submitting ? 'wait' : 'pointer'}}>
            ❌ Cancelar
          </button>
        )}
      </div>
    );
  }

  window.ZNX = window.ZNX || {};
  window.ZNX.widgets = window.ZNX.widgets || {};
  window.ZNX.widgets.quotes = window.ZNX.widgets.quotes || {};
  window.ZNX.widgets.quotes.QuoteStatusActions = QuoteStatusActions;
})();
