# -*- coding: utf-8 -*-
"""
Script de mise à jour propre de blocnotes.html :
1. Déploiement d'un onglet DÉDIÉ "🎨 Usine Studio Publicité" en position 2.
2. Déblocage du scrolling universel fluide (overflow-y: auto !important, touch-scrolling).
3. Synchronisation automatique sur public_jarvis/ et public/.
"""

with open(r'C:\ASSISTANT_IA\JARVIS_LIVE_VOICE\public_jarvis\blocnotes.html', 'r', encoding='utf-8') as f:
    html = f.read()

# 1. Remplacer CSS html, body pour scrolling ultra-fluide
old_css = '''    html, body {
      background: #080b11;
      color: #e2e8f0;
      font-family: 'Outfit', -apple-system, sans-serif;
      margin: 0;
      padding: 0;
      width: 100%;
      max-width: 100vw;
      overflow-x: hidden;
      touch-action: pan-y;
    }
    .bn-container {
      width: 100%;
      max-width: 1100px;
      margin: 0 auto;
      padding: 12px 14px 40px 14px;
    }'''

new_css = '''    html {
      height: 100%;
      overflow-y: auto !important;
      -webkit-overflow-scrolling: touch !important;
    }
    body {
      background: #080b11;
      color: #e2e8f0;
      font-family: 'Outfit', -apple-system, sans-serif;
      margin: 0;
      padding: 0;
      width: 100%;
      min-height: 100%;
      overflow-y: auto !important;
      overflow-x: hidden !important;
      -webkit-overflow-scrolling: touch !important;
      touch-action: auto !important;
    }
    .bn-container {
      width: 100%;
      max-width: 1100px;
      margin: 0 auto;
      padding: 12px 14px 120px 14px;
      min-height: 100%;
      overflow-y: visible !important;
    }'''

if old_css in html:
    html = html.replace(old_css, new_css)
    print("1. CSS html/body mis à jour.")
else:
    print("1. old_css non trouvé, vérification alternative...")

# 2. Remplacer CSS tab-content et md-container
old_tab_css = '''    .tab-content {
      display: none;
      width: 100%;
      max-width: 100%;
      overflow-x: hidden;
    }'''

new_tab_css = '''    .tab-content {
      display: none;
      width: 100%;
      max-width: 100%;
      overflow-y: visible !important;
      overflow-x: hidden !important;
      -webkit-overflow-scrolling: touch !important;
    }'''

if old_tab_css in html:
    html = html.replace(old_tab_css, new_tab_css)
    print("2. CSS tab-content mis à jour.")

# 3. Ajouter l'onglet Studio Publicité dans la barre de navigation
old_tabs_nav = '''        <button class="tab-btn active" onclick="switchTab('dossiers')">📁 Dossiers Projets Clés</button>
        <button class="tab-btn" onclick="switchTab('galerie')">🏛️ Galerie des Boutiques</button>'''

new_tabs_nav = '''        <button class="tab-btn active" onclick="switchTab('dossiers', this)">📁 Dossiers Projets Clés</button>
        <button class="tab-btn" onclick="switchTab('studio', this)" style="background: rgba(168, 85, 247, 0.2); border: 1.5px solid #a855f7; color: #d8b4fe; font-weight: 700;">🎨 Usine Studio Publicité</button>
        <button class="tab-btn" onclick="switchTab('galerie', this)">🏛️ Galerie des Boutiques</button>'''

if old_tabs_nav in html:
    html = html.replace(old_tabs_nav, new_tabs_nav)
    print("3. Onglet Studio Publicité ajouté dans la nav.")
else:
    print("3. old_tabs_nav non trouvé!")

# Remplacer les autres switchTab pour passer 'this'
for tab in ['chantiers', 'produits', 'saga', 'cultivez', 'championnes', 'handball']:
    html = html.replace(f"switchTab('{tab}')", f"switchTab('{tab}', this)")

# 4. Insérer le contenu de tab-studio juste avant tab-galerie
target_galerie = '    <!-- ======================================================== -->\n    <!-- ONGLET 1 BIS : GALERIE DES BOUTIQUES CRÉÉES             -->'

tab_studio_content = '''    <!-- ======================================================== -->
    <!-- ONGLET DÉDIÉ : USINE STUDIO PUBLICITÉ & MÉDIAS           -->
    <!-- ======================================================== -->
    <div id="tab-studio" class="tab-content">
      <div style="background: rgba(168, 85, 247, 0.12); border-left: 4px solid #a855f7; padding: 14px 18px; border-radius: 0 10px 10px 0; margin-bottom: 20px;">
        <h2 style="margin: 0; font-size: 1.25rem; color: #d8b4fe; display: flex; align-items: center; gap: 8px;">
          🎨 Usine Studio Publicité &amp; Médias — Dossier Centralisé
        </h2>
        <p style="margin: 6px 0 0 0; font-size: 0.88rem; color: #e2e8f0; line-height: 1.5;">
          Tous les éléments directeurs pour la production automatique d'actifs publicitaires et de médias de boutique : cahier des charges, scripts UGC, avatars référents, bannières statiques et vidéos démo.
        </p>
      </div>

      <div class="project-card" style="border-color: rgba(168, 85, 247, 0.4);">
        <div class="project-card-header">
          <div>
            <div class="project-title" style="font-size: 1.25rem; color: #f8fafc;">
              🎬 Les 6 Pôles de Production de l'Usine Studio
            </div>
            <div style="font-size: 0.82rem; color: #94a3b8; margin-top: 4px;">
              Dossier source : <code>C:\\ASSISTANT_IA\\PROJET_STUDIO_PUB\\</code> • Moteurs : fal.ai, Pillow &amp; FFmpeg
            </div>
          </div>
          <span class="badge-status" style="background: rgba(168, 85, 247, 0.2); color: #d8b4fe; border: 1px solid rgba(168, 85, 247, 0.4);">
            🛠️ Cadrage &amp; Passation Validés
          </span>
        </div>

        <div class="resource-grid">
          <!-- 1. Cadrage DAG -->
          <a href="/artifacts/CADRAGE_ARCHITECTURE_USINE_STUDIO_CREATIF.html" class="resource-link" target="_blank" style="border-color: rgba(168, 85, 247, 0.5);">
            <div class="res-top">
              <span class="res-name" style="color: #c084fc;">📐 Cadrage &amp; Architecture DAG</span>
              <span class="res-type">Spécifications</span>
            </div>
            <div class="res-desc">Architecture en 6 pôles (Packshots, Lifestyle, Micro-Vidéos, Statiques, UGC, Avatars) et coûts fal.ai.</div>
            <button class="btn-audio-mini" onclick="event.preventDefault(); event.stopPropagation(); playAudioBrief('cadrage_studio', this);" data-title="Cadrage Usine Studio">🎧 Écouter le cadrage</button>
          </a>

          <!-- 2. Passation Claude Code -->
          <a href="/artifacts/DOSSIER_PASSATION_CLAUDE_CODE_USINE_STUDIO_CREATIF.html" class="resource-link" target="_blank" style="border-color: rgba(245, 158, 11, 0.5);">
            <div class="res-top">
              <span class="res-name" style="color: #f59e0b;">🤝 Passation Claude Code</span>
              <span class="res-type" style="color: #f59e0b;">Brief Relais</span>
            </div>
            <div class="res-desc">Synthèse des acquis Usine Boutique et feuille de route pour le Master Plan et tickets T01-T60.</div>
            <button class="btn-audio-mini" onclick="event.preventDefault(); event.stopPropagation(); playAudioBrief('passation_claude', this);" data-title="Passation Claude Code">🎧 Écouter la passation</button>
          </a>

          <!-- 3. Catalogue fal.ai -->
          <a href="/artifacts/Catalogue_Modeles_Generation_Visuelle.md" class="resource-link" target="_blank">
            <div class="res-top">
              <span class="res-name">🧰 Catalogue fal.ai Certifié</span>
              <span class="res-type">Guide Modèles</span>
            </div>
            <div class="res-desc">Bria Eraser, Bria Background Remove, Nano Banana Pro Edit, Qwen Image Edit et Kling 3.0 Pro I2V.</div>
            <button class="btn-audio-mini" onclick="event.preventDefault(); event.stopPropagation(); playAudioBrief('catalogue_fal', this);" data-title="Catalogue fal.ai">🎧 Écouter le catalogue</button>
          </a>

          <!-- 4. Scripts UGC Centralisés -->
          <a href="/artifacts/SCRIPT_UGC_SNEAKSAFE_BASE.html" class="resource-link" target="_blank">
            <div class="res-top">
              <span class="res-name">🎬 Scripts UGC &amp; Hooks</span>
              <span class="res-type">UGC Centralisé</span>
            </div>
            <div class="res-desc">Fichier centralisé unique contenant les accroches, scripts publicitaires et angles d'attaque validés.</div>
            <button class="btn-audio-mini" onclick="event.preventDefault(); event.stopPropagation(); playAudioBrief('scripts_ugc', this);" data-title="Scripts UGC Centralisés">🎧 Écouter les accroches</button>
          </a>

          <!-- 5. Bannières Statiques -->
          <a href="/artifacts/CADRAGE_ARCHITECTURE_USINE_STUDIO_CREATIF.html" class="resource-link" target="_blank">
            <div class="res-top">
              <span class="res-name">🎯 Publicités Statiques Meta</span>
              <span class="res-type">Pillow 1:1 &amp; 9:16</span>
            </div>
            <div class="res-desc">Générateur de créatives à fort taux de clic avec badges réassurance et hooks contrastés.</div>
            <button class="btn-audio-mini" onclick="event.preventDefault(); event.stopPropagation(); playAudioBrief('cadrage_studio', this);" data-title="Créatives Statiques">🎧 Écouter</button>
          </a>

          <!-- 6. Avatars & Swap -->
          <a href="/artifacts/CADRAGE_ARCHITECTURE_USINE_STUDIO_CREATIF.html" class="resource-link" target="_blank" style="border-color: rgba(56, 189, 248, 0.4);">
            <div class="res-top">
              <span class="res-name" style="color: #38bdf8;">🧑‍🤝‍🧑 Avatars &amp; Product Swap</span>
              <span class="res-type">Méthode Arcads</span>
            </div>
            <div class="res-desc">Standard de référence : vraie personne, anonymisation faciale et swap de produit par inpainting.</div>
            <button class="btn-audio-mini" onclick="event.preventDefault(); event.stopPropagation(); playAudioBrief('cadrage_studio', this);" data-title="Avatars Référents">🎧 Écouter</button>
          </a>
        </div>
      </div>
    </div>\n\n'''

if target_galerie in html:
    html = html.replace(target_galerie, tab_studio_content + target_galerie)
    print("4. Contenu tab-studio inséré avec succès.")
else:
    print("4. target_galerie non trouvé!")

# 5. Mettre à jour switchTab JS pour un scroll top automatique
old_switch = '''    function switchTab(tabId) {
      document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
      document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
      
      event.currentTarget.classList.add('active');
      document.getElementById('tab-' + tabId).classList.add('active');
    }'''

new_switch = '''    function switchTab(tabId, btnEl) {
      document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
      document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
      
      if (btnEl) {
        btnEl.classList.add('active');
      } else if (window.event && window.event.currentTarget) {
        window.event.currentTarget.classList.add('active');
      }
      
      const target = document.getElementById('tab-' + tabId);
      if (target) {
        target.classList.add('active');
      }
      window.scrollTo({ top: 0, behavior: 'smooth' });
      document.body.scrollTop = 0;
      document.documentElement.scrollTop = 0;
    }'''

if old_switch in html:
    html = html.replace(old_switch, new_switch)
    print("5. switchTab JS mis à jour.")
else:
    print("5. old_switch non trouvé!")

# Sauvegarde
for out_path in [r'C:\ASSISTANT_IA\JARVIS_LIVE_VOICE\public_jarvis\blocnotes.html', r'C:\ASSISTANT_IA\JARVIS_LIVE_VOICE\public\blocnotes.html']:
    with open(out_path, 'w', encoding='utf-8') as f:
        f.write(html)
    print("Fichier synchronisé :", out_path)
