{% extends "base.html" %}
{% block title %}
  {% if recipe %}
    {{ recipe.code }} — {{ _('Modifica Ricetta') }}
  {% else %}
    {{ _('Nuova Ricetta') }}
  {% endif %}
   — TieMeasureFlow
{% endblock %}

{% block extra_head %}
<style>
  .drop-zone {
    border: 2px dashed var(--border-color);
    border-radius: 0.75rem;
    transition: border-color 0.2s ease, background-color 0.2s ease;
  }
  .drop-zone.drag-over {
    border-color: var(--color-primary);
    background-color: rgba(37, 99, 235, 0.05);
  }
  .dark .drop-zone.drag-over {
    background-color: rgba(59, 130, 246, 0.08);
  }
</style>
{% endblock %}

{% block content %}
{# ——— Determine mode flags ——— #}
{% set is_new = (recipe is none) %}
{% set recipe_id = recipe.id if recipe else None %}
{% set current_version = recipe.current_version if recipe and recipe.current_version else None %}
{% set versions = recipe.versions if recipe and recipe.versions else [] %}

<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-8 max-w-4xl"
     x-data="recipeEditor()"
     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 class="font-medium text-[var(--text-primary)]">
        {% if is_new %}
          {{ _('Nuova Ricetta') }}
        {% else %}
          {{ recipe.code }}
        {% endif %}
      </li>
    </ol>
  </nav>

  <!-- ============================================================
       Header + Action Buttons
       ============================================================ -->
  <div class="mb-8">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-3">
      <div>
        <h1 class="text-3xl font-bold text-[var(--text-primary)] mb-1">
          {% if is_new %}
            {{ _('Nuova Ricetta') }}
          {% else %}
            <span class="font-mono text-primary">{{ recipe.code }}</span>
            <span class="text-[var(--text-muted)] text-lg font-normal ml-2">
              (v<span x-text="currentVersion"></span>)
            </span>
          {% endif %}
        </h1>
        {% if not is_new %}
        <p class="text-sm text-[var(--text-secondary)]" x-text="name"></p>
        {% else %}
        <p class="text-sm text-[var(--text-secondary)]">
          {{ _('Compila i dati della ricetta') }}
        </p>
        {% endif %}
      </div>

      <!-- Top action buttons -->
      <div class="flex items-center gap-2 shrink-0 flex-wrap">
        <button @click="saveRecipe()"
                :disabled="saving || !name.trim() || (isNew && !code.trim())"
                class="btn btn-primary gap-1.5">
          <svg x-show="!saving" 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="M5 13l4 4L19 7"/>
          </svg>
          <svg x-show="saving" class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24">
            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
          </svg>
          <span x-text="saving ? '{{ _('Salvataggio...') }}' : '{{ _('Salva') }}'"></span>
        </button>

        {% if not is_new %}
        <a href="{{ url_for('maker.recipe_preview', recipe_id=recipe_id) }}"
           class="btn btn-secondary gap-1.5">
          <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="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') }}
        </a>

        <a href="{{ url_for('maker.task_editor', recipe_id=recipe_id) }}"
           class="btn btn-secondary gap-1.5">
          <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="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 2m-6 9l2 2 4-4"/>
          </svg>
          {{ _('Task') }}
        </a>
        {% endif %}

        <a href="{{ url_for('maker.recipe_list') }}"
           class="btn btn-secondary gap-1.5">
          <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="M6 18L18 6M6 6l12 12"/>
          </svg>
          {{ _('Annulla') }}
        </a>
      </div>
    </div>
  </div>

  <!-- ============================================================
       Error Banner
       ============================================================ -->
  <div x-show="errorMessage"
       x-transition
       class="mb-6 flex items-center gap-3 px-4 py-3 rounded-lg
              bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800
              text-red-800 dark:text-red-200 text-sm">
    <svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
            d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
    </svg>
    <span x-text="errorMessage"></span>
    <button @click="errorMessage = ''" class="ml-auto shrink-0 hover:opacity-70">
      <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
      </svg>
    </button>
  </div>

  <!-- ============================================================
       Success Banner
       ============================================================ -->
  <div x-show="saved"
       x-transition
       x-init="$watch('saved', v => { if (v) setTimeout(() => saved = false, 4000) })"
       class="mb-6 flex items-center gap-3 px-4 py-3 rounded-lg
              bg-emerald-50 dark:bg-emerald-900/30 border border-emerald-200 dark:border-emerald-800
              text-emerald-800 dark:text-emerald-200 text-sm">
    <svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
            d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
    </svg>
    <span>{{ _('Ricetta salvata con successo') }}</span>
  </div>

  <!-- ============================================================
       1. Card: Metadati Ricetta
       ============================================================ -->
  <div class="tmf-card mb-6">
    <div class="tmf-card-header flex items-center gap-2">
      <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 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>
      {{ _('Metadati Ricetta') }}
    </div>
    <div class="tmf-card-body">
      <div class="grid grid-cols-1 sm:grid-cols-2 gap-5">
        <!-- Codice -->
        <div>
          <label class="tmf-label" for="recipe-code">
            {{ _('Codice') }} <span class="text-red-500">*</span>
          </label>
          <input id="recipe-code"
                 type="text"
                 x-model="code"
                 class="tmf-input font-mono tracking-wide"
                 :disabled="!isNew"
                 :class="{ 'opacity-60 cursor-not-allowed': !isNew }"
                 placeholder="{{ _('Es. COUPLING-256') }}"
                 required>
          <p x-show="!isNew" class="text-xs text-[var(--text-muted)] mt-1">
            {{ _('Il codice non puo essere modificato dopo la creazione') }}
          </p>
        </div>

        <!-- Nome -->
        <div>
          <label class="tmf-label" for="recipe-name">
            {{ _('Nome') }} <span class="text-red-500">*</span>
          </label>
          <input id="recipe-name"
                 type="text"
                 x-model="name"
                 class="tmf-input"
                 placeholder="{{ _('Es. Coupling Assembly 256') }}"
                 required>
        </div>

        <!-- Descrizione -->
        <div class="sm:col-span-2">
          <label class="tmf-label" for="recipe-description">
            {{ _('Descrizione') }}
          </label>
          <textarea id="recipe-description"
                    x-model="description"
                    class="tmf-input"
                    rows="3"
                    placeholder="{{ _('Descrizione opzionale della ricetta...') }}"></textarea>
        </div>

        <!-- Intervallo di misura -->
        <div>
          <label class="tmf-label" for="recipe-interval">
            {{ _('Intervallo misura (minuti)') }}
          </label>
          <input id="recipe-interval"
                 type="number"
                 x-model.number="measurementIntervalMinutes"
                 class="tmf-input"
                 min="1"
                 max="1440"
                 placeholder="{{ _('Es. 30') }}">
          <p class="text-xs text-[var(--text-muted)] mt-1">
            {{ _('Timer cicalino per ricordare la misurazione periodica') }}
          </p>
        </div>
      </div>
    </div>
  </div>

  <!-- ============================================================
       2. Card: Immagine Anteprima (thumbnail ricetta)
       ============================================================ -->
  <div class="tmf-card mb-6">
    <div class="tmf-card-header flex items-center justify-between">
      <div class="flex items-center gap-2">
        <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="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>
        {{ _('Immagine Anteprima') }}
      </div>
      <span x-show="currentFilePath" class="badge badge-pass text-xs">
        <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="M5 13l4 4L19 7"/>
        </svg>
        {{ _('Immagine caricata') }}
      </span>
    </div>

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

      <!-- Current image preview -->
      <template x-if="currentFilePath">
        <div class="space-y-3">
          <div class="relative rounded-lg overflow-hidden bg-[var(--bg-secondary)] border border-[var(--border-color)]">
            <img x-bind:src="'/maker/api/files/' + currentFilePath"
                 class="block mx-auto max-h-64 object-contain p-2"
                 :class="{ 'opacity-40': uploadingFile }"
                 loading="lazy">
            <!-- Upload overlay on image -->
            <div x-show="uploadingFile"
                 class="absolute inset-0 flex flex-col items-center justify-center bg-white/60 dark:bg-black/50">
              <svg class="w-8 h-8 animate-spin text-primary" fill="none" viewBox="0 0 24 24">
                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
              </svg>
              <span class="text-sm font-medium text-[var(--text-primary)] mt-2">{{ _('Caricamento in corso...') }}</span>
            </div>
          </div>
          <!-- Action buttons -->
          <div class="flex items-center gap-2 flex-wrap">
            <label class="btn btn-secondary text-sm gap-1.5 cursor-pointer"
                   :class="{ 'opacity-50 pointer-events-none': uploadingFile }">
              <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="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"/>
              </svg>
              {{ _('Sostituisci') }}
              <input type="file"
                     accept="image/*,application/pdf"
                     class="hidden"
                     :disabled="uploadingFile"
                     @change="uploadFile($event.target.files[0]); $event.target.value = ''">
            </label>
            <button @click.stop="currentFilePath = ''"
                    type="button"
                    :disabled="uploadingFile"
                    class="btn btn-secondary text-sm gap-1.5 text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20">
              <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="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
              </svg>
              {{ _('Rimuovi') }}
            </button>
          </div>
        </div>
      </template>

      <!-- Upload area (only when no image) -->
      <template x-if="!currentFilePath">
        <div class="drop-zone p-6 text-center cursor-pointer relative"
             :class="{ 'drag-over': dragOver }"
             @dragover.prevent="dragOver = true"
             @dragleave.prevent="dragOver = false"
             @drop.prevent="handleDrop($event)"
             @click="$refs.fileInput.click()">

          <input type="file"
                 x-ref="fileInput"
                 @change="uploadFile($event.target.files[0]); $event.target.value = ''"
                 accept="image/*,application/pdf"
                 class="hidden">

          <!-- Uploading spinner -->
          <div x-show="uploadingFile" class="flex flex-col items-center gap-3 py-4">
            <svg class="w-8 h-8 animate-spin text-primary" fill="none" viewBox="0 0 24 24">
              <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
              <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
            </svg>
            <span class="text-sm text-[var(--text-secondary)]">{{ _('Caricamento in corso...') }}</span>
          </div>

          <!-- Upload prompt -->
          <div x-show="!uploadingFile" class="flex flex-col items-center gap-3 py-4">
            <div class="w-12 h-12 rounded-full bg-primary-50 dark:bg-primary-900/30 flex items-center justify-center">
              <svg class="w-6 h-6 text-primary" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round"
                      d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5"/>
              </svg>
            </div>
            <div class="text-center">
              <span class="inline-block px-4 py-2 rounded-lg bg-primary text-white text-sm font-semibold mb-2">
                {{ _('Carica Immagine') }}
              </span>
              <p class="text-xs text-[var(--text-muted)]">
                {{ _('Trascina qui oppure clicca. Formati: PNG, JPG, WebP, PDF (max 50MB)') }}
              </p>
              <p class="text-xs text-[var(--text-muted)] mt-1">
                {{ _('Usata come thumbnail nella lista ricette') }}
              </p>
            </div>
          </div>
        </div>
      </template>


    </div>
  </div>

  <!-- ============================================================
       3. Card: Versioning (solo in modifica)
       ============================================================ -->
  {% if not is_new %}
  <div class="tmf-card mb-6">
    <div class="tmf-card-header flex items-center gap-2">
      <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="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
      </svg>
      {{ _('Versioning') }}
    </div>
    <div class="tmf-card-body space-y-4">
      <!-- Current version info -->
      <div class="flex items-center gap-3 flex-wrap">
        <span class="text-sm text-[var(--text-secondary)]">
          {{ _('Versione corrente:') }}
        </span>
        <span class="badge badge-neutral font-mono font-semibold"
              x-text="'v' + currentVersion">
        </span>
        <template x-if="versions.length > 1">
          <a href="{{ url_for('maker.version_history', recipe_id=recipe_id) }}"
             class="text-xs text-primary hover:underline">
            {{ _('Vedi cronologia') }} (<span x-text="versions.length"></span> {{ _('versioni') }})
          </a>
        </template>
      </div>

      <!-- Auto-version notice -->
      <div class="flex items-start gap-2 p-3 rounded-lg bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800">
        <svg class="w-5 h-5 text-blue-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>
        <p class="text-xs text-blue-700 dark:text-blue-300">
          {{ _('Se modifichi questa ricetta verra creata automaticamente la versione') }}
          <strong x-text="'v' + (currentVersion + 1)"></strong>.
          {{ _('Le misure esistenti resteranno associate alla versione corrente.') }}
        </p>
      </div>

      <!-- Change notes -->
      <div>
        <label class="tmf-label" for="change-notes">
          {{ _('Motivo della modifica') }}
        </label>
        <input id="change-notes"
               type="text"
               x-model="changeNotes"
               class="tmf-input"
               placeholder="{{ _('Es. Aggiornate tolleranze foro centrale...') }}">
        <p class="text-xs text-[var(--text-muted)] mt-1">
          {{ _('Opzionale. Verra registrato nella cronologia versioni.') }}
        </p>
      </div>
    </div>
  </div>
  {% endif %}

  <!-- ============================================================
       3. Footer Action Buttons
       ============================================================ -->
  <div class="flex flex-col sm:flex-row items-center gap-3 pt-4 pb-8">
    <button @click="saveRecipe()"
            :disabled="saving || !name.trim() || (isNew && !code.trim())"
            class="btn btn-primary gap-2 w-full sm:w-auto justify-center">
      <svg x-show="!saving" 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="M5 13l4 4L19 7"/>
      </svg>
      <svg x-show="saving" class="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24">
        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
      </svg>
      <span x-text="saving ? '{{ _('Salvataggio...') }}' : '{{ _('Salva Ricetta') }}'"></span>
    </button>

    <a href="{{ url_for('maker.recipe_list') }}"
       class="btn btn-secondary gap-2 w-full sm:w-auto justify-center">
      <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="M6 18L18 6M6 6l12 12"/>
      </svg>
      {{ _('Annulla') }}
    </a>
  </div>

</div>
{% endblock %}

{% block extra_js %}
<script>
function recipeEditor() {
  return {
    // ---- Mode ----
    isNew: {{ 'true' if recipe is none else 'false' }},
    recipeId: {{ recipe.id|tojson if recipe else 'null' }},

    // ---- Form data ----
    code: {{ (recipe.code if recipe else '')|tojson }},
    name: {{ (recipe.name if recipe else '')|tojson }},
    description: {{ (recipe.description if recipe and recipe.description else '')|tojson }},
    changeNotes: '',

    // ---- Save state ----
    saving: false,
    saved: false,
    errorMessage: '',

    // ---- Versioning ----
    versions: {{ (recipe.versions if recipe and recipe.versions else [])|tojson }},
    currentVersion: {{ (recipe.current_version.version_number if recipe and recipe.current_version else 0)|tojson }},

    // ---- Measurement interval ----
    measurementIntervalMinutes: {{ (recipe.measurement_interval_minutes if recipe and recipe.measurement_interval_minutes else 'null')|tojson }},

    // ---- File upload (preview image) ----
    currentFilePath: {{ (recipe.image_path if recipe and recipe.image_path else '')|tojson }},
    uploadingFile: false,
    dragOver: false,

    // ============================================================
    // Save Recipe
    // ============================================================
    async saveRecipe() {
      this.saving = true;
      this.errorMessage = '';
      this.saved = false;

      const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;

      const payload = {
        code: this.code.trim(),
        name: this.name.trim(),
        description: this.description.trim(),
      };

      // Measurement interval
      if (this.measurementIntervalMinutes) {
        payload.measurement_interval_minutes = parseInt(this.measurementIntervalMinutes, 10);
      }

      // Include image_path for preview thumbnail
      if (this.currentFilePath) {
        payload.image_path = this.currentFilePath;
      }

      // Include change notes if editing
      if (!this.isNew && this.changeNotes.trim()) {
        payload.change_notes = this.changeNotes.trim();
      }

      const url = this.isNew
        ? '/maker/api/recipes'
        : '/maker/api/recipes/' + this.recipeId;
      const method = this.isNew ? 'POST' : 'PUT';

      try {
        const resp = await fetch(url, {
          method,
          headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrfToken || ''
          },
          body: JSON.stringify(payload)
        });

        const data = await resp.json();

        if (data.error) {
          var detail = data.detail || data.error || {{ _("Errore nel salvataggio")|tojson }};
          this.errorMessage = (typeof detail === 'string') ? detail : JSON.stringify(detail);
          this.saving = false;
          return;
        }

        if (this.isNew) {
          // Redirect to task editor after creating a new recipe
          window.location.href = '/maker/recipes/' + data.id + '/tasks';
          return;
        }

        // Update local state on successful edit
        this.saved = true;
        this.changeNotes = '';

        // Update version info if server returned it
        if (data.current_version) {
          this.currentVersion = data.current_version.version_number;
        }

        // Refresh versions list if available
        if (data.versions) {
          this.versions = data.versions;
        }

      } catch (err) {
        console.error('Save error:', err);
        this.errorMessage = {{ _("Errore di connessione al server")|tojson }};
      }

      this.saving = false;
    },

    // ============================================================
    // Navigation
    // ============================================================
    goToPreview() {
      if (this.recipeId) {
        window.location.href = '/maker/recipes/' + this.recipeId + '/preview';
      }
    },

    goToTasks() {
      if (this.recipeId) {
        window.location.href = '/maker/recipes/' + this.recipeId + '/tasks';
      }
    },

    // ============================================================
    // File Upload (thumbnail)
    // ============================================================
    handleDrop(event) {
      this.dragOver = false;
      const files = event.dataTransfer?.files;
      if (files && files.length > 0) {
        this.uploadFile(files[0]);
      }
    },

    async uploadFile(file) {
      if (!file) return;

      // Validate file type
      const allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'application/pdf'];
      if (!allowed.includes(file.type)) {
        this.errorMessage = {{ _("Formato file non supportato. Usa PNG, JPG, GIF, WebP o PDF.")|tojson }};
        return;
      }

      // Validate size (50MB)
      if (file.size > 50 * 1024 * 1024) {
        this.errorMessage = {{ _("File troppo grande. Massimo 50MB.")|tojson }};
        return;
      }

      this.uploadingFile = true;
      this.errorMessage = '';

      const csrfToken = document.querySelector('meta[name=csrf-token]')?.content;

      try {
        const formData = new FormData();
        formData.append('file', file);
        if (this.recipeId) {
          formData.append('recipe_id', this.recipeId);
        }

        const resp = await fetch('/maker/api/upload', {
          method: 'POST',
          headers: { 'X-CSRFToken': csrfToken || '' },
          body: formData
        });

        const data = await resp.json();

        if (data.error) {
          this.errorMessage = data.detail || {{ _("Errore nel caricamento del file")|tojson }};
          this.uploadingFile = false;
          return;
        }

        this.currentFilePath = data.file_path;

      } catch (err) {
        console.error('Upload error:', err);
        this.errorMessage = {{ _("Errore di connessione durante l'upload")|tojson }};
      }

      this.uploadingFile = false;
    }
  };
}
</script>
{% endblock %}
