import { Observable, forkJoin, of } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
import { CampaignService } from '../services/campaign.service';
import { TreeItem } from '../services/layout.service';
import { Arc, Chapter, Scene } from '../services/campaign.model';

/**
 * Helper — charge l'arborescence complète d'une campagne (arcs -> chapitres -> scènes)
 * et la transforme en TreeItem[] pour la secondary sidebar.
 *
 * Pourquoi un helper et pas un service ? C'est de la logique de présentation
 * (mapping REST -> ViewModel de la sidebar), pas du domaine métier.
 */

export interface CampaignTreeData {
  arcs: Arc[];
  chaptersByArc: Record<string, Chapter[]>;
  scenesByChapter: Record<string, Scene[]>;
}

export function loadCampaignTreeData(
  service: CampaignService,
  campaignId: string
): Observable<CampaignTreeData> {
  return service.getArcs(campaignId).pipe(
    switchMap(arcs => {
      if (arcs.length === 0) {
        return of({ arcs, chaptersByArc: {}, scenesByChapter: {} });
      }
      const chapterCalls = arcs.map(a =>
        service.getChapters(a.id!).pipe(map(chapters => ({ arcId: a.id!, chapters })))
      );
      return forkJoin(chapterCalls).pipe(
        switchMap(chapterResults => {
          const chaptersByArc: Record<string, Chapter[]> = {};
          const allChapters: Chapter[] = [];
          chapterResults.forEach(r => {
            chaptersByArc[r.arcId] = r.chapters;
            allChapters.push(...r.chapters);
          });

          if (allChapters.length === 0) {
            return of({ arcs, chaptersByArc, scenesByChapter: {} });
          }
          const sceneCalls = allChapters.map(c =>
            service.getScenes(c.id!).pipe(map(scenes => ({ chapterId: c.id!, scenes })))
          );
          return forkJoin(sceneCalls).pipe(
            map(sceneResults => {
              const scenesByChapter: Record<string, Scene[]> = {};
              sceneResults.forEach(r => { scenesByChapter[r.chapterId] = r.scenes; });
              return { arcs, chaptersByArc, scenesByChapter };
            })
          );
        })
      );
    })
  );
}

export function buildCampaignTree(campaignId: string, data: CampaignTreeData): TreeItem[] {
  return data.arcs.map(arc => {
    const chapterItems: TreeItem[] = (data.chaptersByArc[arc.id!] ?? []).map(ch => {
      const sceneItems: TreeItem[] = (data.scenesByChapter[ch.id!] ?? []).map(sc => ({
        id: sc.id!,
        label: sc.name,
        route: `/campaigns/${campaignId}/arcs/${arc.id}/chapters/${ch.id}/scenes/${sc.id}`
      }));
      sceneItems.push({
        id: `new-scene-${ch.id}`,
        label: '+ Nouvelle scène',
        isAction: true,
        route: `/campaigns/${campaignId}/arcs/${arc.id}/chapters/${ch.id}/scenes/create`
      });
      return {
        id: ch.id!,
        label: ch.name,
        children: sceneItems,
        route: `/campaigns/${campaignId}/arcs/${arc.id}/chapters/${ch.id}`
      };
    });
    chapterItems.push({
      id: `new-chapter-${arc.id}`,
      label: '+ Nouveau chapitre',
      isAction: true,
      route: `/campaigns/${campaignId}/arcs/${arc.id}/chapters/create`
    });
    return {
      id: arc.id!,
      label: arc.name,
      children: chapterItems,
      route: `/campaigns/${campaignId}/arcs/${arc.id}`
    };
  });
}
