<script>
  import logoUrl from '../assets/logo.png';

  export let state = null;
  export let connection = 'offline';
  export let playerPortrait = 'player-silhouette';

  function trustLabel(score) {
    if (score >= 75) return 'Entrusted';
    if (score >= 65) return 'Reliable';
    if (score >= 55) return 'Settling In';
    return 'Probationary';
  }

  function formatRemaining(seconds) {
    const safe = Math.max(0, Number(seconds ?? 0));
    const minutes = Math.floor(safe / 60);
    const remainder = safe % 60;
    return `${String(minutes).padStart(2, '0')}:${String(remainder).padStart(2, '0')}`;
  }
</script>

<header class="header">
  <div class="brand">
    <img class="brand-logo" src={logoUrl} alt="Axiom Works">
    <div>
      <p class="eyebrow">Axiom Works Internal</p>
      <h1>Sysadmin Chronicles</h1>
    </div>
  </div>

  <div class="status-cluster">
    <div class="trust-card">
      <span class="label">Ops Standing</span>
      <strong>{trustLabel(state?.trust ?? 50)}</strong>
      <div class="meter">
        <span style={`width:${Math.max(8, state?.trust ?? 50)}%`}></span>
      </div>
    </div>

    <div class="meta-card">
      <span class="label">Shift #{state?.shiftNumber ?? 1}</span>
      <strong>{formatRemaining(state?.shift?.remainingSeconds ?? 0)}</strong>
      <span class:online={connection === 'connected'} class="socket">{connection}</span>
    </div>

    <div class="meta-card">
      <span class="label">Certifications</span>
      <strong>{state?.certifications?.length ?? 0}</strong>
      <span class="socket">{state?.shiftHistory?.length ?? 0} reviews archived</span>
    </div>

    <div class="portrait-card">
      <img
        class="portrait-avatar"
        src={`/public/portraits/${playerPortrait}.png`}
        alt="Your portrait"
        onerror={(e) => { e.currentTarget.src = '/public/portraits/player-silhouette.png'; }}
      >
    </div>
  </div>
</header>

<style>
  .header {
    display: flex;
    justify-content: space-between;
    gap: 1.5rem;
    align-items: flex-end;
    margin-bottom: 1.5rem;
  }

  .eyebrow,
  .label,
  .socket {
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-size: 0.72rem;
    color: var(--text-muted);
  }

  h1 {
    margin: 0.2rem 0 0;
    font-size: clamp(2rem, 3vw, 3rem);
    line-height: 0.95;
  }

  .brand {
    display: flex;
    align-items: center;
    gap: 1rem;
  }

  .brand-logo {
    height: 48px;
    width: 48px;
    border-radius: 10px;
    flex-shrink: 0;
  }

  .status-cluster {
    display: grid;
    grid-template-columns: repeat(3, minmax(170px, 1fr)) 64px;
    gap: 0.8rem;
    min-width: min(480px, 100%);
  }

  .portrait-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 0.5rem;
    box-shadow: var(--shadow);
    backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .portrait-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255,255,255,0.15);
  }

  .trust-card,
  .meta-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 1rem;
    box-shadow: var(--shadow);
    backdrop-filter: blur(12px);
  }

  strong {
    display: block;
    margin-top: 0.25rem;
    font-size: 1.05rem;
  }

  .meter {
    margin-top: 0.8rem;
    height: 10px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.08);
    overflow: hidden;
  }

  .meter span {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, #b06c37, var(--accent));
  }

  .socket {
    display: inline-flex;
    margin-top: 0.8rem;
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.06);
  }

  .socket.online {
    color: #dfeedd;
    background: rgba(99, 182, 122, 0.16);
  }

  @media (max-width: 900px) {
    .header {
      flex-direction: column;
      align-items: stretch;
    }

    .status-cluster {
      min-width: 0;
    }
  }
</style>
