<div class="edit-page">

  <div class="page-header">
    <div>
      <h1>{{ chapter?.name || ('chapterEdit.chapter' | translate) }}</h1>
      <p class="subtitle">{{ 'chapterEdit.chapter' | translate }}</p>
    </div>
    <div class="header-actions">
      <button type="button" class="btn-ai"
        (click)="toggleChat()"
        [class.active]="chatOpen"
        [title]="'chapterEdit.aiAssistantTitle' | translate">
        <lucide-icon [img]="Sparkles" [size]="14"></lucide-icon>
        {{ 'chapterEdit.aiAssistant' | translate }}
      </button>
      <button type="button" class="btn-secondary" (click)="cancel()">{{ 'common.cancel' | translate }}</button>
      <button type="button" class="btn-danger" (click)="delete()">
        <lucide-icon [img]="Trash2" [size]="14"></lucide-icon>
        {{ 'common.delete' | translate }}
      </button>
      <button type="button" class="btn-primary" (click)="submit()" [disabled]="form.invalid">
        {{ 'common.save' | translate }}
      </button>
    </div>
  </div>

  <form [formGroup]="form" (ngSubmit)="submit()" class="edit-form">

    <!-- Conditions de déblocage (scénario) — pour TOUT chapitre : arc HUB (= quête)
    comme arc linéaire (= chapitre conditionnel). Vide = disponible d'emblée. -->
    <div class="hub-section">
      <h2 class="hub-section-title">
        {{ (parentArc?.type === 'HUB' ? 'chapterEdit.unlockTitleQuest' : 'chapterEdit.unlockTitleChapter') | translate }}
      </h2>
      <small class="field-hint">
        {{ (parentArc?.type === 'HUB' ? 'chapterEdit.unlockHintQuest' : 'chapterEdit.unlockHintChapter') | translate }}
      </small>
      <app-prerequisite-editor
        [prerequisites]="prerequisites"
        [availableQuests]="availableQuests"
        [availableFlags]="availableFlagNames"
        (prerequisitesChange)="onPrerequisitesChange($event)">
      </app-prerequisite-editor>
    </div>

    <!-- Illustrations (galerie editable, rendu editorial) -->
    <div class="field">
      <label>{{ 'chapterEdit.illustrations' | translate }}</label>
      <app-image-gallery
        [imageIds]="illustrationImageIds"
        [editable]="true"
        [layout]="'EDITORIAL'"
        (imageIdsChange)="illustrationImageIds = $event">
      </app-image-gallery>
      <small class="field-hint">{{ 'chapterEdit.illustrationsHint' | translate }}</small>
    </div>


    <div class="field">
      <label for="chapter-edit-name">{{ 'chapterEdit.nameLabel' | translate }}</label>
      <input
        id="chapter-edit-name"
        type="text"
        formControlName="name"
        [placeholder]="'chapterEdit.namePlaceholder' | translate"
        [class.invalid]="form.get('name')?.invalid && form.get('name')?.touched"
        />
    </div>

    <div class="field">
      <label for="chapter-edit-description">{{ 'chapterEdit.synopsisLabel' | translate }}</label>
      <textarea
        id="chapter-edit-description"
        formControlName="description"
        [placeholder]="'chapterEdit.synopsisPlaceholder' | translate"
        rows="5">
      </textarea>
    </div>

    <div class="field">
      <label>{{ 'chapterEdit.icon' | translate }}</label>
      <app-icon-picker [options]="campaignIconOptions" [(selected)]="selectedIcon"></app-icon-picker>
    </div>

    <div class="field">
      <label for="chapter-edit-gm-notes">{{ 'chapterEdit.gmNotesLabel' | translate }}</label>
      <textarea
        id="chapter-edit-gm-notes"
        formControlName="gmNotes"
        [placeholder]="'chapterEdit.gmNotesPlaceholder' | translate"
        rows="6">
      </textarea>
      <small class="field-hint">{{ 'chapterEdit.gmNotesHint' | translate }}</small>
    </div>

    <div class="field-row">
      <div class="field">
        <label for="chapter-edit-player-objectives">{{ 'chapterEdit.playerObjectivesLabel' | translate }}</label>
        <textarea
          id="chapter-edit-player-objectives"
          formControlName="playerObjectives"
          [placeholder]="'chapterEdit.playerObjectivesPlaceholder' | translate"
          rows="4">
        </textarea>
      </div>
      <div class="field">
        <label for="chapter-edit-narrative-stakes">{{ 'chapterEdit.narrativeStakesLabel' | translate }}</label>
        <textarea
          id="chapter-edit-narrative-stakes"
          formControlName="narrativeStakes"
          [placeholder]="'chapterEdit.narrativeStakesPlaceholder' | translate"
          rows="4">
        </textarea>
      </div>
    </div>

    <!-- ===== Pages Lore associées (B2 cross-context) ===== -->
    @if (loreId) {
      <div class="field">
        <label>{{ 'chapterEdit.relatedPages' | translate }}</label>
        <app-lore-link-picker
          [value]="relatedPageIds"
          [availablePages]="availablePages"
          [loreId]="loreId"
          (valueChange)="relatedPageIds = $event">
        </app-lore-link-picker>
        <small class="field-hint">
          {{ 'chapterEdit.relatedPagesHint' | translate }}
        </small>
      </div>
    }

    @if (!loreId) {
      <div class="field">
        <small class="field-hint">
          {{ 'chapterEdit.noLoreHint' | translate }}
        </small>
      </div>
    }

  </form>

</div>

<!-- Drawer chat IA (hors .edit-page pour couvrir le viewport à droite) -->
<app-ai-chat-drawer
  [campaignId]="campaignId"
  entityType="chapter"
  [entityId]="chapterId"
  [isOpen]="chatOpen"
  [welcomeMessage]="'chapterEdit.chatWelcome' | translate"
  [quickSuggestions]="chatQuickSuggestions"
  (close)="chatOpen = false">
</app-ai-chat-drawer>
