<template>
  <div class="product-header">
    <div class="header-left">
      <div class="thumb-wrap">
        <img
          v-if="product.imageUrl && !imgError"
          :src="product.imageUrl"
          class="thumb"
          @error="imgError = true"
        />
        <div v-else class="thumb-placeholder">JV</div>
      </div>
      <div class="header-info">
        <div class="header-kicker">Research target</div>
        <div class="product-name-row">
          <span class="product-name">{{ product.name }}</span>
          <a
            v-if="product.productUrl"
            :href="product.productUrl"
            target="_blank"
            rel="noopener"
            class="product-link"
            title="View on JustVitamins"
          >Open source page</a>
        </div>
        <div class="product-meta">
          <span class="meta-item"><span class="meta-label">SKU</span> {{ product.sku }}</span>
          <span class="meta-sep">·</span>
          <span class="meta-item"><span class="meta-label">Reviews</span> {{ product.totalReviews?.toLocaleString() }}</span>
          <span class="meta-sep">·</span>
          <span class="meta-item"><span class="meta-label">Updated</span> {{ today }}</span>
        </div>
      </div>
    </div>

    <div class="header-right">
      <div class="header-stat">
        <strong>{{ product.hasIntelligence ? 'Ready' : 'Pending' }}</strong>
        <span>research state</span>
      </div>
      <div class="header-stat">
        <strong>Top-3</strong>
        <span>scrape focus</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
const props = defineProps({ product: Object })
const imgError = ref(false)
const today = new Date().toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' })
watch(() => props.product?.sku, () => { imgError.value = false })
</script>

<style scoped>
.product-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: var(--productheader-height);
  background: var(--header-dark);
  flex-shrink: 0;
  gap: 16px;
  overflow: hidden;
}

.header-left { display: flex; align-items: center; gap: 14px; min-width: 0; }

.thumb-wrap {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  overflow: hidden;
  background: #1f1f1f;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.thumb { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.thumb-placeholder { font-size: 12px; font-weight: 700; color: #fff; }

.header-info { min-width: 0; }
.header-kicker {
  font-size: 10px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #8da2fb;
  margin-bottom: 4px;
}
.product-name-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.product-name {
  font-size: 14px;
  font-weight: 600;
  color: #ffffff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 560px;
  line-height: 1.3;
}
.product-link {
  font-size: 11px;
  color: #c7d2fe;
  white-space: nowrap;
  opacity: 0.9;
  flex-shrink: 0;
}
.product-link:hover { opacity: 1; }

.product-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  flex-wrap: nowrap;
}
.meta-item { font-size: 11px; color: #b5b5b5; white-space: nowrap; }
.meta-label { color: #7a7a7a; margin-right: 3px; }
.meta-sep { color: #474747; font-size: 11px; }

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.header-stat {
  min-width: 110px;
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  padding: 8px 10px;
  background: rgba(255,255,255,0.02);
}
.header-stat strong {
  display: block;
  color: #fff;
  font-size: 12px;
}
.header-stat span {
  display: block;
  color: #8b8b8b;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-top: 2px;
}
</style>
