<script lang="ts">
  import type { MixCalculatorPreview, MixCalculatorSession } from '$lib/types';

  let {
    preview,
    sessionNumber = null
  }: {
    preview: MixCalculatorPreview | MixCalculatorSession | null;
    sessionNumber?: string | null;
  } = $props();

  function formatDate(value: string) {
    return new Intl.DateTimeFormat('en-NZ', {
      dateStyle: 'medium'
    }).format(new Date(value));
  }

  function formatNumber(value: number | null | undefined, digits = 2) {
    if (value === null || value === undefined) {
      return 'N/A';
    }

    return value.toFixed(digits);
  }
</script>

<article class="result-card">
  <div class="section-header">
    <div>
      <h3>Calculated Output</h3>
      <p>{preview ? 'Snapshot of the scaled raw material requirements.' : 'Run the calculation to preview the session output.'}</p>
    </div>
    {#if sessionNumber}
      <div class="session-chip">
        <span>Session</span>
        <strong>{sessionNumber}</strong>
      </div>
    {/if}
  </div>

  {#if preview}
    <div class="metric-row">
      <article class="metric-card">
        <span>Total kg</span>
        <strong>{formatNumber(preview.total_kg, 2)}</strong>
        <p>Scaled batch size</p>
      </article>
      <article class="metric-card">
        <span>Total bags</span>
        <strong>{formatNumber(preview.total_bags, 2)}</strong>
        <p>{preview.product_unit_of_measure}</p>
      </article>
      <article class="metric-card">
        <span>Prepared by</span>
        <strong>{preview.prepared_by_name}</strong>
        <p>{formatDate(preview.mix_date)}</p>
      </article>
    </div>

    {#if preview.warnings.length}
      <div class="warning-stack">
        {#each preview.warnings as warning}
          <p>{warning}</p>
        {/each}
      </div>
    {/if}

    <div class="summary-grid">
      <div>
        <span>Client</span>
        <strong>{preview.client_name}</strong>
      </div>
      <div>
        <span>Product</span>
        <strong>{preview.product_name}</strong>
      </div>
      <div>
        <span>Mix source</span>
        <strong>{preview.mix_name}</strong>
      </div>
      <div>
        <span>Unit size</span>
        <strong>{formatNumber(preview.product_unit_size_kg, 2)}kg</strong>
      </div>
    </div>

    <div class="table-wrap">
      <table>
        <thead>
          <tr>
            <th>Raw material</th>
            <th>Mix %</th>
            <th>Required kg</th>
            <th>Unit</th>
          </tr>
        </thead>
        <tbody>
          {#each preview.lines as line}
            <tr>
              <td data-label="Raw material">
                <strong>{line.raw_material_name}</strong>
              </td>
              <td data-label="Mix %">{formatNumber(line.mix_percentage, 2)}%</td>
              <td data-label="Required kg">{formatNumber(line.required_kg, 2)}kg</td>
              <td data-label="Unit">{line.unit}</td>
            </tr>
          {/each}
        </tbody>
      </table>
    </div>
  {:else}
    <div class="empty-state">
      <div class="empty-shimmer-metrics">
        <div class="shimmer-metric"><div class="shimmer-line short"></div><div class="shimmer-line wide"></div></div>
        <div class="shimmer-metric"><div class="shimmer-line short"></div><div class="shimmer-line wide"></div></div>
        <div class="shimmer-metric"><div class="shimmer-line short"></div><div class="shimmer-line wide"></div></div>
      </div>
      <div class="empty-state-copy">
        <div class="empty-icon" aria-hidden="true">
          <span></span><span></span><span></span>
        </div>
        <strong>No calculation yet</strong>
        <span>Choose a client, product, date, and batch size on the left, then click Calculate mix.</span>
      </div>
      <div class="empty-shimmer-rows">
        {#each [1,2,3,4,5] as _}
          <div class="shimmer-row">
            <div class="shimmer-line wide"></div>
            <div class="shimmer-line medium"></div>
            <div class="shimmer-line medium"></div>
            <div class="shimmer-line short"></div>
          </div>
        {/each}
      </div>
    </div>
  {/if}
</article>

<style>
  h3,
  p {
    margin: 0;
  }

  .section-header p,
  .metric-card p,
  .summary-grid span,
  .empty-state span {
    color: var(--muted);
  }

  .result-card,
  .metric-card {
    border: 1px solid var(--line);
    border-radius: 1.3rem;
    background: var(--panel);
    box-shadow: var(--shadow);
  }

  .result-card {
    padding: 1.2rem;
  }

  .section-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1rem;
  }

  .section-header h3 {
    font-size: 1.15rem;
    font-weight: 700;
  }

  .session-chip {
    display: grid;
    gap: 0.14rem;
    padding: 0.72rem 0.82rem;
    border: 1px solid var(--line);
    border-radius: 0.92rem;
    background: var(--panel-soft);
  }

  .session-chip span {
    color: var(--muted);
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
  }

  .metric-row,
  .summary-grid {
    display: grid;
    gap: 0.85rem;
  }

  .metric-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    margin-bottom: 1rem;
  }

  .metric-card {
    padding: 1rem;
  }

  .metric-card span {
    display: block;
    color: var(--muted);
    font-size: 0.84rem;
  }

  .metric-card strong {
    display: block;
    margin: 0.45rem 0 0.18rem;
    font-size: 1.45rem;
  }

  .warning-stack {
    display: grid;
    gap: 0.45rem;
    margin-top: 1rem;
    padding: 0.92rem;
    border-radius: 1rem;
    background: #fff6e6;
    color: #8b5b1e;
  }

  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-bottom: 1rem;
  }

  .summary-grid div {
    padding: 0.88rem 0.92rem;
    border: 1px solid var(--line);
    border-radius: 1rem;
    background: var(--panel-soft);
  }

  .summary-grid span {
    display: block;
    margin-bottom: 0.2rem;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
  }

  .table-wrap {
    overflow-x: auto;
  }

  table {
    width: 100%;
    min-width: 30rem;
    border-collapse: collapse;
  }

  th,
  td {
    padding: 0.9rem 0.85rem;
    text-align: left;
    border-bottom: 1px solid var(--line);
  }

  th {
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .empty-state {
    display: flex;
    flex-direction: column;
    gap: 0;
    border-radius: 1rem;
    overflow: hidden;
    border: 1px solid var(--line);
  }

  .empty-shimmer-metrics {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.75rem;
    padding: 1rem;
    border-bottom: 1px solid var(--line);
    background: var(--panel-soft);
  }

  .shimmer-metric {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.85rem;
    border: 1px solid var(--line);
    border-radius: 0.85rem;
    background: var(--panel);
  }

  .empty-state-copy {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 2rem 1.5rem;
    text-align: center;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
  }

  .empty-state-copy strong {
    font-size: 0.98rem;
    font-weight: 700;
    color: var(--text);
  }

  .empty-state-copy span {
    max-width: 26rem;
    font-size: 0.84rem;
    line-height: 1.5;
  }

  .empty-icon {
    display: flex;
    align-items: flex-end;
    gap: 0.28rem;
    height: 2.2rem;
    margin-bottom: 0.35rem;
  }

  .empty-icon span {
    width: 0.38rem;
    border-radius: 999px 999px 0 0;
    background: var(--color-border);
    animation: bar-pulse 1.6s ease-in-out infinite;
  }

  .empty-icon span:nth-child(1) { height: 60%; animation-delay: 0s; }
  .empty-icon span:nth-child(2) { height: 100%; animation-delay: 0.2s; }
  .empty-icon span:nth-child(3) { height: 40%; animation-delay: 0.4s; }

  @keyframes bar-pulse {
    0%, 100% { opacity: 0.35; }
    50% { opacity: 1; }
  }

  .empty-shimmer-rows {
    display: flex;
    flex-direction: column;
    background: var(--panel-soft);
  }

  .shimmer-row {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 0.75fr;
    gap: 1rem;
    align-items: center;
    padding: 0.78rem 1rem;
    border-bottom: 1px solid var(--line);
  }

  .shimmer-row:last-child {
    border-bottom: none;
  }

  .shimmer-line {
    height: 0.7rem;
    border-radius: 999px;
    background: linear-gradient(
      90deg,
      var(--color-border) 25%,
      color-mix(in srgb, var(--color-border) 40%, white) 50%,
      var(--color-border) 75%
    );
    background-size: 200% 100%;
    animation: shimmer 1.8s ease-in-out infinite;
  }

  .shimmer-line.short { width: 40%; }
  .shimmer-line.medium { width: 65%; }
  .shimmer-line.wide { width: 90%; }

  @keyframes shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
  }

  @media (max-width: 980px) {
    .metric-row {
      grid-template-columns: 1fr;
    }
  }

  @media (max-width: 720px) {
    .section-header {
      flex-direction: column;
    }

    .summary-grid {
      grid-template-columns: 1fr;
    }

    table,
    thead,
    tbody,
    tr,
    td {
      display: block;
      width: 100%;
    }

    thead {
      display: none;
    }

    tbody {
      display: grid;
      gap: 0.75rem;
    }

    tbody tr {
      border: 1px solid var(--line);
      border-radius: 1rem;
      background: var(--panel-soft);
      overflow: hidden;
    }

    tbody td {
      border-bottom: 1px solid var(--line);
    }

    tbody td:last-child {
      border-bottom: none;
    }

    tbody td::before {
      content: attr(data-label);
      display: block;
      margin-bottom: 0.24rem;
      color: var(--muted);
      font-size: 0.72rem;
      font-weight: 700;
      letter-spacing: 0.06em;
      text-transform: uppercase;
    }
  }
</style>
