<?php
$tips = $tips ?? [];
$kind = $kind ?? 'sales';
$ask = $ask ?? null;
if (!smart_tips_on() || !$tips) {
    return;
}
$renderRows = static function (array $rows): void {
    foreach ($rows as $row):
        $tone = (string) ($row['tone'] ?? 'now');
        ?>
        <a class="tip-row tip-<?= e($tone) ?>" href="<?= e((string) $row['url']) ?>">
          <span class="tip-row-ico"><i class="bi <?= $tone === 'warn' ? 'bi-exclamation-triangle-fill' : 'bi-lightning-fill' ?>"></i></span>
          <span class="min-w-0">
            <span class="tip-row-title"><?= e((string) $row['title']) ?></span>
            <span class="tip-row-why"><?= e((string) $row['why']) ?></span>
          </span>
          <i class="bi bi-chevron-right tip-go"></i>
        </a>
        <?php
    endforeach;
};
?>
<section class="tip-panel mb-0" id="smart-tips">
  <div class="tip-head">
    <span class="tip-badge"><i class="bi bi-lightbulb-fill"></i> Smart tip</span>
    <p class="tip-summary mb-0"><?= e((string) ($tips['summary'] ?? '')) ?></p>
  </div>

  <?php if ($kind === 'sales'): ?>
    <?php if (!empty($tips['call_today'])): ?>
      <h3 class="tip-h">Call this person today</h3>
      <?php $renderRows($tips['call_today']); ?>
    <?php endif; ?>
    <?php if (!empty($tips['cold'])): ?>
      <h3 class="tip-h">Deal going cold</h3>
      <?php $renderRows($tips['cold']); ?>
    <?php endif; ?>
    <?php if (empty($tips['call_today']) && empty($tips['cold']) && !empty($tips['need_step'])): ?>
      <h3 class="tip-h">Next step</h3>
      <?php $renderRows(array_slice($tips['need_step'], 0, 4)); ?>
    <?php endif; ?>
  <?php endif; ?>

  <?php if ($kind === 'manager'): ?>
    <?php if (!empty($tips['risk_deals'])): ?>
      <h3 class="tip-h">Deals at risk</h3>
      <?php $renderRows($tips['risk_deals']); ?>
    <?php endif; ?>
    <?php if (!empty($tips['risk_accounts'])): ?>
      <h3 class="tip-h">Customers at risk</h3>
      <?php $renderRows($tips['risk_accounts']); ?>
    <?php endif; ?>
    <form class="tip-ask" action="<?= e(url('dashboard')) ?>" method="get">
      <label class="form-label" for="tipAsk">Ask a simple question</label>
      <div class="tip-ask-row">
        <input class="form-control" id="tipAsk" name="ask" value="<?= e((string) ($ask['q'] ?? '')) ?>" placeholder="cold deals, renewals, late visits" maxlength="80" autocomplete="off">
        <button class="btn btn-primary" type="submit">Show list</button>
      </div>
      <div class="tip-hints">
        <?php foreach (($ask['hints'] ?? \App\Services\SmartTipsService::ASK_HINTS) as $q => $label): ?>
          <a class="tip-chip" href="<?= e(url('dashboard') . '?ask=' . rawurlencode($q)) ?>"><?= e($q) ?></a>
        <?php endforeach; ?>
      </div>
    </form>
    <?php if ($ask && ($ask['q'] ?? '') !== ''): ?>
      <h3 class="tip-h"><?= e((string) ($ask['label'] ?: 'Results')) ?></h3>
      <?php if (empty($ask['rows'])): ?>
        <p class="tip-empty">No matches. Use a short phrase like “cold deals” or “renewals”.</p>
      <?php else: ?>
        <?php $renderRows($ask['rows']); ?>
      <?php endif; ?>
    <?php endif; ?>
  <?php endif; ?>

  <?php if ($kind === 'service'): ?>
    <?php $recap = $tips['recap'] ?? null; ?>
    <?php if ($recap): ?>
      <h3 class="tip-h">Before this visit</h3>
      <a class="tip-recap" href="<?= e((string) $recap['url']) ?>">
        <div class="tip-recap-kicker">This visit</div>
        <div class="tip-recap-name"><?= e((string) $recap['name']) ?></div>
        <div class="tip-recap-meta">
          <span><i class="bi bi-clock"></i> <?= e((string) $recap['when']) ?></span>
          <?php if ($recap['place'] !== ''): ?><span><i class="bi bi-geo-alt"></i> <?= e((string) $recap['place']) ?></span><?php endif; ?>
          <?php if ($recap['phone'] !== ''): ?><span><i class="bi bi-telephone"></i> <?= e((string) $recap['phone']) ?></span><?php endif; ?>
        </div>
        <div class="tip-recap-facts">
          <?php if ($recap['last_visit'] !== ''): ?><span>Last visit <?= e((string) $recap['last_visit']) ?></span><?php endif; ?>
          <?php if ((int) $recap['jobs'] > 0): ?><span><?= (int) $recap['jobs'] ?> open <?= (int) $recap['jobs'] === 1 ? 'job' : 'jobs' ?></span><?php endif; ?>
          <?php if ((int) $recap['equipment'] > 0): ?><span><?= (int) $recap['equipment'] ?> machines</span><?php endif; ?>
        </div>
        <?php if ($recap['last_note'] !== ''): ?>
          <div class="tip-recap-note"><?= e((string) $recap['last_note']) ?></div>
        <?php endif; ?>
      </a>
    <?php endif; ?>
    <?php if (!empty($tips['followup'])): ?>
      <h3 class="tip-h">Suggested follow-up</h3>
      <?php $renderRows($tips['followup']); ?>
    <?php endif; ?>
  <?php endif; ?>

  <?php if ($kind === 'marketing'): ?>
    <h3 class="tip-h">Better lists from opens and clicks</h3>
    <div class="tip-suggest">
      <?php foreach (($tips['suggestions'] ?? []) as $s): ?>
        <a class="tip-suggest-card" href="<?= e((string) $s['url']) ?>">
          <span class="tip-suggest-n"><?= (int) $s['count'] ?></span>
          <span class="tip-suggest-l"><?= e((string) $s['label']) ?></span>
          <span class="tip-row-why"><?= e((string) $s['why']) ?></span>
        </a>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</section>
<?php if ($kind === 'manager' && trim((string) ($ask['q'] ?? '')) !== ''): ?>
<script>document.getElementById('smart-tips')?.scrollIntoView({behavior:'smooth',block:'start'});</script>
<?php endif; ?>
