{% extends "base.html" %}
{% block title %}{{ recipe.code }} — {{ _('Anteprima Ricetta') }} — TieMeasureFlow{% endblock %}

{% block extra_head %}
<style>
  /* Annotation viewer container */
  .annotation-container {
    position: relative;
    width: 100%;
    min-height: 300px;
    background: var(--bg-secondary);
    border-radius: 0.5rem;
    overflow: hidden;
  }
  .annotation-container canvas {
    display: block;
    max-width: 100%;
    height: auto;
    margin: 0 auto;
  }

  /* Tolerance bar gradient */
  .tolerance-bar-bg {
    background: linear-gradient(to right,
      rgba(220, 38, 38, 0.15) 0%,
      rgba(217, 119, 6, 0.15) 15%,
      rgba(5, 150, 105, 0.15) 30%,
      rgba(5, 150, 105, 0.15) 70%,
      rgba(217, 119, 6, 0.15) 85%,
      rgba(220, 38, 38, 0.15) 100%
    );
  }
</style>
{% endblock %}

{% block content %}
<script>
  window.__taskAnnotations = {};
  {% for task in recipe.tasks %}
  {% if task.annotations_json %}
  window.__taskAnnotations[{{ task.id }}] = {{ task.annotations_json|tojson }};
  {% endif %}
  {% endfor %}
</script>
<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-8 max-w-5xl"
     x-data="recipePreview()"
     x-cloak>

  {# ================================================================
     BREADCRUMB
     ================================================================ #}
  <nav class="mb-6" aria-label="Breadcrumb">
    <ol class="flex items-center gap-2 text-sm text-[var(--text-secondary)]">
      <li>
        <a href="{{ url_for('maker.recipe_list') }}"
           class="hover:text-primary transition-colors inline-flex items-center gap-1">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="1.75" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round"
                  d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
          </svg>
          {{ _('Ricette') }}
        </a>
      </li>
      <li>
        <svg class="w-4 h-4 text-[var(--text-muted)]" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"/>
        </svg>
      </li>
      <li>
        <a href="{{ url_for('maker.recipe_edit', recipe_id=recipe.id) }}"
           class="hover:text-primary transition-colors">
          {{ recipe.code }}
        </a>
      </li>
      <li>
        <svg class="w-4 h-4 text-[var(--text-muted)]" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"/>
        </svg>
      </li>
      <li class="font-medium text-[var(--text-primary)]">
        {{ _('Anteprima') }}
      </li>
    </ol>
  </nav>

  {# ================================================================
     HEADER
     ================================================================ #}
  <div class="mb-6">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-3">
      <div class="flex items-center gap-3 flex-wrap">
        {# Preview badge #}
        <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider
                     bg-amber-100 dark:bg-amber-900/30 text-amber-700 dark:text-amber-300
                     border border-amber-300 dark:border-amber-700">
          <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round"
                  d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
            <path stroke-linecap="round" stroke-linejoin="round"
                  d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
          </svg>
          {{ _('Anteprima') }}
        </span>

        <div>
          <h1 class="text-2xl font-bold text-[var(--text-primary)]">
            <span class="font-mono text-primary">{{ recipe.code }}</span>
            <span class="text-[var(--text-muted)] text-lg font-normal ml-1">
              &mdash; {{ recipe.name }}
            </span>
          </h1>
          {% if recipe.current_version %}
          <p class="text-xs text-[var(--text-secondary)] mt-0.5">
            {{ _('Versione') }} <span class="font-mono font-semibold">v{{ recipe.current_version.version_number }}</span>
          </p>
          {% endif %}
        </div>
      </div>

      {# Action buttons #}
      <div class="flex items-center gap-2 shrink-0 flex-wrap">
        <a href="{{ url_for('maker.recipe_edit', recipe_id=recipe.id) }}"
           class="btn btn-secondary gap-1.5 text-sm">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
          </svg>
          {{ _('Torna a Ricetta') }}
        </a>
        <a href="{{ url_for('maker.task_editor', recipe_id=recipe.id) }}"
           class="btn btn-secondary gap-1.5 text-sm">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round"
                  d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
          </svg>
          {{ _('Modifica Task') }}
        </a>
      </div>
    </div>
  </div>

  {# ================================================================
     PREVIEW BANNER
     ================================================================ #}
  <div class="mb-6 flex items-start gap-3 px-4 py-3 rounded-lg
              bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800">
    <svg class="w-5 h-5 text-amber-500 shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
            d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
    </svg>
    <div>
      <p class="text-sm font-medium text-amber-800 dark:text-amber-200">
        {{ _('Modalita Anteprima') }}
      </p>
      <p class="text-xs text-amber-700 dark:text-amber-300 mt-0.5">
        {{ _('Stai vedendo la ricetta come la vedra il Tecnico di Misura. Tutti i campi sono in sola lettura.') }}
      </p>
    </div>
  </div>

  {# ================================================================
     RECIPE DESCRIPTION (if present)
     ================================================================ #}
  {% if recipe.description %}
  <div class="tmf-card mb-6">
    <div class="tmf-card-body">
      <p class="text-sm text-[var(--text-secondary)] leading-relaxed">{{ recipe.description }}</p>
    </div>
  </div>
  {% endif %}

  {# ================================================================
     TASKS LOOP
     ================================================================ #}
  {% if recipe.tasks %}
    {% for task in recipe.tasks %}
    <div class="tmf-card mb-6">

      {# ---- Task Header ---- #}
      <div class="tmf-card-header flex items-center justify-between">
        <div class="flex items-center gap-3">
          <span class="inline-flex items-center justify-center w-8 h-8 rounded-lg
                       bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-300
                       border border-primary-200 dark:border-primary-800
                       text-sm font-bold">
            {{ loop.index }}
          </span>
          <div>
            <h2 class="font-semibold text-[var(--text-primary)]">{{ task.title }}</h2>
            {% if task.directive or task.description %}
            <p class="text-xs text-[var(--text-secondary)] mt-0.5">
              {{ task.directive or task.description }}
            </p>
            {% endif %}
          </div>
        </div>
        {% if task.subtasks %}
        <span class="badge badge-neutral text-xs font-mono">
          {{ task.subtasks|length }} {{ _('misure') }}
        </span>
        {% endif %}
      </div>

      <div class="tmf-card-body space-y-5">

        {# ---- Technical Drawing / Annotations ---- #}
        {% if task.file_path and task.file_type == 'image' %}
        <div>
          <div class="flex items-center gap-2 mb-2 text-xs font-medium text-[var(--text-secondary)]">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="1.75" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round"
                    d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
            </svg>
            {{ _('Disegno Tecnico') }}
          </div>
          <div class="annotation-container"
               x-data="annotationViewer()"
               x-init="
                 imageUrl = '/maker/api/files/{{ task.file_path }}';
                 annotations = window.__taskAnnotations[{{ task.id }}] || null;
                 $nextTick(() => init());
               ">
            <canvas x-ref="annotationCanvas"></canvas>
          </div>
        </div>

        {% elif task.file_path and task.file_type == 'pdf' %}
        <div class="flex items-center gap-3 p-4 rounded-lg bg-[var(--bg-secondary)] border border-[var(--border-color)]">
          <svg class="w-10 h-10 text-red-400 shrink-0" fill="none" stroke="currentColor" stroke-width="1" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round"
                  d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>
          </svg>
          <div class="flex-1">
            <p class="text-sm font-medium text-[var(--text-primary)]">{{ _('Documento PDF allegato') }}</p>
            <p class="text-xs text-[var(--text-muted)]">{{ task.file_path }}</p>
          </div>
          <a href="/maker/api/files/{{ task.file_path }}"
             target="_blank"
             class="btn btn-secondary text-xs gap-1.5">
            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round"
                    d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"/>
            </svg>
            {{ _('Apri PDF') }}
          </a>
        </div>

        {% else %}
        <div class="flex items-center justify-center p-6 rounded-lg bg-[var(--bg-secondary)] border border-[var(--border-color)]">
          <div class="text-center">
            <svg class="w-10 h-10 mx-auto text-[var(--text-muted)] mb-2" fill="none" stroke="currentColor" stroke-width="1" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round"
                    d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
            </svg>
            <p class="text-sm text-[var(--text-muted)]">{{ _('Nessuna immagine allegata') }}</p>
          </div>
        </div>
        {% endif %}

        {# ---- Subtask List ---- #}
        {% if task.subtasks %}
        <div>
          <div class="flex items-center gap-2 mb-3 text-xs font-medium text-[var(--text-secondary)]">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="1.75" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round"
                    d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
            </svg>
            {{ _('Punti di Misura') }}
          </div>

          <div class="space-y-3">
            {% for subtask in task.subtasks %}
            <div class="p-4 rounded-lg border border-[var(--border-color)] bg-[var(--bg-card)]
                        hover:border-primary/30 transition-colors">
              {# Subtask header: marker badge + description #}
              <div class="flex items-start gap-3 mb-3">
                <span class="inline-flex items-center justify-center w-8 h-8 rounded-full shrink-0
                             bg-primary text-white font-bold text-sm shadow-sm">
                  {{ subtask.marker_number }}
                </span>
                <div class="flex-1 min-w-0">
                  <p class="font-medium text-sm text-[var(--text-primary)]">{{ subtask.description }}</p>
                  <div class="flex items-center gap-2 mt-0.5 text-xs text-[var(--text-muted)]">
                    {% if subtask.measurement_type %}
                    <span>{{ subtask.measurement_type }}</span>
                    <span>&middot;</span>
                    {% endif %}
                    <span>{{ subtask.unit or 'mm' }}</span>
                  </div>
                </div>
              </div>

              {# Tolerance parameters grid #}
              <div class="grid grid-cols-2 sm:grid-cols-5 gap-2">
                {# Nominal #}
                <div class="col-span-2 sm:col-span-1 flex items-center justify-between sm:flex-col sm:items-center
                            py-2 px-3 rounded-lg bg-primary-50 dark:bg-primary-900/20
                            border border-primary-100 dark:border-primary-800">
                  <span class="text-[10px] font-medium text-primary-600 dark:text-primary-400 uppercase tracking-wider">
                    {{ _('Nominale') }}
                  </span>
                  <span class="measure-value text-sm font-bold text-primary">
                    {% if subtask.nominal is not none %}{{ "%.3f"|format(subtask.nominal) }}{% else %}&mdash;{% endif %}
                  </span>
                </div>

                {# LTL #}
                <div class="flex items-center justify-between sm:flex-col sm:items-center
                            py-2 px-3 rounded-lg bg-red-50/50 dark:bg-red-900/10">
                  <span class="text-[10px] font-medium text-measure-fail uppercase tracking-wider">LTL</span>
                  <span class="measure-value text-xs font-semibold text-measure-fail">
                    {% if subtask.ltl is not none %}{{ "%.3f"|format(subtask.ltl) }}{% else %}&mdash;{% endif %}
                  </span>
                </div>

                {# LWL #}
                <div class="flex items-center justify-between sm:flex-col sm:items-center
                            py-2 px-3 rounded-lg bg-amber-50/50 dark:bg-amber-900/10">
                  <span class="text-[10px] font-medium text-measure-warning uppercase tracking-wider">LWL</span>
                  <span class="measure-value text-xs font-semibold text-measure-warning">
                    {% if subtask.lwl is not none %}{{ "%.3f"|format(subtask.lwl) }}{% else %}&mdash;{% endif %}
                  </span>
                </div>

                {# UWL #}
                <div class="flex items-center justify-between sm:flex-col sm:items-center
                            py-2 px-3 rounded-lg bg-amber-50/50 dark:bg-amber-900/10">
                  <span class="text-[10px] font-medium text-measure-warning uppercase tracking-wider">UWL</span>
                  <span class="measure-value text-xs font-semibold text-measure-warning">
                    {% if subtask.uwl is not none %}{{ "%.3f"|format(subtask.uwl) }}{% else %}&mdash;{% endif %}
                  </span>
                </div>

                {# UTL #}
                <div class="flex items-center justify-between sm:flex-col sm:items-center
                            py-2 px-3 rounded-lg bg-red-50/50 dark:bg-red-900/10">
                  <span class="text-[10px] font-medium text-measure-fail uppercase tracking-wider">UTL</span>
                  <span class="measure-value text-xs font-semibold text-measure-fail">
                    {% if subtask.utl is not none %}{{ "%.3f"|format(subtask.utl) }}{% else %}&mdash;{% endif %}
                  </span>
                </div>
              </div>

              {# Visual tolerance bar #}
              {% if subtask.ltl is not none and subtask.utl is not none %}
              <div class="mt-2">
                <div class="tolerance-bar-bg h-2 rounded-full relative overflow-hidden border border-[var(--border-color)]">
                  {# Nominal center line #}
                  <div class="absolute top-0 bottom-0 w-px bg-primary/40" style="left: 50%;"></div>
                </div>
                <div class="flex justify-between mt-0.5 text-[9px] font-mono text-[var(--text-muted)]">
                  <span>{{ "%.2f"|format(subtask.ltl) }}</span>
                  <span>{{ "%.2f"|format(subtask.nominal or 0) }}</span>
                  <span>{{ "%.2f"|format(subtask.utl) }}</span>
                </div>
              </div>
              {% endif %}
            </div>
            {% endfor %}
          </div>
        </div>
        {% else %}
        <div class="text-center py-4">
          <p class="text-sm text-[var(--text-muted)]">{{ _('Nessun punto di misura definito per questo task') }}</p>
        </div>
        {% endif %}

      </div>
    </div>
    {% endfor %}

  {% else %}
    {# ---- Empty state: no tasks ---- #}
    <div class="tmf-card">
      <div class="p-12 text-center">
        <div class="inline-flex items-center justify-center w-16 h-16 rounded-full
                    bg-[var(--bg-secondary)] mb-4">
          <svg class="w-8 h-8 text-[var(--text-muted)]" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round"
                  d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
          </svg>
        </div>
        <h3 class="text-lg font-semibold text-[var(--text-primary)] mb-2">
          {{ _('Nessun task definito') }}
        </h3>
        <p class="text-sm text-[var(--text-secondary)] mb-4">
          {{ _('Questa ricetta non contiene ancora task di misurazione.') }}
        </p>
        <a href="{{ url_for('maker.task_editor', recipe_id=recipe.id) }}"
           class="btn btn-primary gap-2 inline-flex">
          <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"/>
          </svg>
          {{ _('Aggiungi Task') }}
        </a>
      </div>
    </div>
  {% endif %}

  {# ================================================================
     FOOTER NAVIGATION
     ================================================================ #}
  <div class="flex flex-col sm:flex-row items-center justify-between gap-3 pt-6 pb-8">
    <a href="{{ url_for('maker.recipe_edit', recipe_id=recipe.id) }}"
       class="btn btn-secondary gap-2 w-full sm:w-auto justify-center">
      <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
      </svg>
      {{ _('Torna a Ricetta') }}
    </a>

    <div class="flex items-center gap-2 w-full sm:w-auto">
      <a href="{{ url_for('maker.task_editor', recipe_id=recipe.id) }}"
         class="btn btn-primary gap-2 flex-1 sm:flex-initial justify-center">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round"
                d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
        </svg>
        {{ _('Modifica Task') }}
      </a>
      <a href="{{ url_for('maker.version_history', recipe_id=recipe.id) }}"
         class="btn btn-secondary gap-2 flex-1 sm:flex-initial justify-center">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round"
                d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
        </svg>
        {{ _('Storico Versioni') }}
      </a>
    </div>
  </div>

</div>
{% endblock %}

{% block extra_js %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
<script>
  pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
</script>
<script src="{{ url_for('static', filename='js/annotation-viewer.js') }}?v=6"></script>
<script>
/**
 * Recipe Preview - Minimal Alpine.js component
 * Read-only preview mode, no editing capabilities.
 */
function recipePreview() {
  return {
    recipe: {{ recipe|tojson }},
    tasks: {{ recipe.tasks|tojson if recipe.tasks else '[]' }},

    init() {
      // Nothing to do - this is a read-only preview.
      // annotation-viewer.js components are initialized inline via x-init.
    }
  };
}
</script>
{% endblock %}
