import { Injectable } from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs'; import { Campaign, CampaignCreate, Arc, ArcCreate, Chapter, ChapterCreate, Scene, SceneCreate, Quest, Playthrough } from './campaign.model'; import { CampaignReadinessAssessment } from './readiness.model'; import { Npc } from './npc.model'; import { RandomTable } from './random-table.model'; import { Enemy } from './enemy.model'; import { PdfExportOptions } from './pdf-export.model'; /** * Arbre de campagne AGRÉGÉ (une seule requête HTTP) — miroir de CampaignTreeDTO. * Remplace la rafale d'appels (~15-20) que la sidebar déclenchait à chaque navigation. */ export interface CampaignTreeResponse { arcs: Arc[]; chaptersByArc: Record; scenesByChapter: Record; npcs: Npc[]; randomTables: RandomTable[]; enemies: Enemy[]; quests: Quest[]; /** Parties (Playthroughs) de la campagne — nœud « Parties » de l'arbre. */ playthroughs?: Playthrough[]; readiness: CampaignReadinessAssessment; } /** * Périmètre de l'export Foundry (modale) : * - maps : Scenes Foundry (battlemaps) + acteurs/tokens des ennemis liés. * - journals : journaux narratifs (arcs, chapitres, scènes, PNJ, bestiaire). * - tables : tables aléatoires (RollTables). */ export interface FoundryExportOptions { maps: boolean; journals: boolean; tables: boolean; } export { PDF_SECTIONS } from './pdf-export.model'; export type { PdfSection, PdfExportOptions } from './pdf-export.model'; /** * Fiche « Publication » d'une campagne (module à vendre ou à partager) : couverture, * page technique, introduction pour le MJ et page de crédits du PDF. Texte libre. */ export interface CampaignPublication { subtitle?: string | null; author?: string | null; system?: string | null; levels?: string | null; players?: string | null; duration?: string | null; pitch?: string | null; gmBackground?: string | null; hooks?: string | null; contentWarnings?: string | null; credits?: string | null; legal?: string | null; version?: string | null; coverImageId?: string | null; } /** Compte des entités qui seront supprimées en cascade avec la campagne. */ export interface CampaignDeletionImpact { arcs: number; chapters: number; scenes: number; playthroughs: number; } /** Compte des entités qui seront supprimées en cascade avec un arc. */ export interface ArcDeletionImpact { chapters: number; scenes: number; } /** Compte des scènes qui tomberont avec un chapitre. */ export interface ChapterDeletionImpact { scenes: number; } /** * Service HTTP pour la gestion des Campagnes. * Port de sortie vers le Backend Java (Architecture Hexagonale). */ @Injectable({ providedIn: 'root' }) export class CampaignService { private apiUrl = '/api/campaigns'; constructor(private http: HttpClient) {} getAllCampaigns(): Observable { return this.http.get(this.apiUrl); } getCampaignById(id: string): Observable { return this.http.get(`${this.apiUrl}/${id}`); } /** Sauvegarde la disposition du graphe de campagne (JSON opaque ; '' = disposition auto). */ updateGraphPositions(id: string, positionsJson: string): Observable { return this.http.put(`${this.apiUrl}/${id}/graph-positions`, { positions: positionsJson }); } /** Bilan de préparation (Pilier B) — alimente les pastilles de l'arbre de la sidebar. */ getReadiness(id: string): Observable { return this.http.get(`${this.apiUrl}/${id}/readiness`); } /** Arbre complet de la campagne en UNE requête (sidebar : structure + quêtes + readiness). */ getTree(id: string): Observable { return this.http.get(`${this.apiUrl}/${id}/tree`); } /** * Télécharge le bundle d'export Foundry de la campagne (.zip). * Périmètre optionnel (tout par défaut) : cartes+ennemis / journaux / tables. */ exportFoundry(id: string, opts?: FoundryExportOptions): Observable { let params = new HttpParams(); if (opts) { params = params .set('maps', String(opts.maps)) .set('journals', String(opts.journals)) .set('tables', String(opts.tables)); } return this.http.get(`${this.apiUrl}/${id}/foundry-export`, { responseType: 'blob', params }); } /** Génère et télécharge le livret PDF de la campagne. */ exportPdf(id: string, opts?: PdfExportOptions): Observable { let params = new HttpParams(); if (opts) { params = params .set('edition', opts.edition) .set('format', opts.format) .set('print', String(opts.print)) .set('sections', opts.sections.join(',')) .set('lore', opts.lore); } return this.http.get(`${this.apiUrl}/${id}/pdf-export`, { responseType: 'blob', params }); } getPublication(id: string): Observable { return this.http.get(`${this.apiUrl}/${id}/publication`); } updatePublication(id: string, publication: CampaignPublication): Observable { return this.http.put(`${this.apiUrl}/${id}/publication`, publication); } createCampaign(campaign: CampaignCreate): Observable { return this.http.post(this.apiUrl, campaign); } updateCampaign(id: string, campaign: CampaignCreate): Observable { return this.http.put(`${this.apiUrl}/${id}`, campaign); } deleteCampaign(id: string): Observable { return this.http.delete(`${this.apiUrl}/${id}`); } getCampaignDeletionImpact(id: string): Observable { return this.http.get(`${this.apiUrl}/${id}/deletion-impact`); } // ========== ARC ========== getArcs(campaignId: string): Observable { const params = new HttpParams().set('campaignId', campaignId); return this.http.get('/api/arcs', { params }); } getArcById(id: string): Observable { return this.http.get(`/api/arcs/${id}`); } createArc(payload: ArcCreate): Observable { return this.http.post('/api/arcs', payload); } updateArc(id: string, payload: ArcCreate): Observable { return this.http.put(`/api/arcs/${id}`, payload); } deleteArc(id: string): Observable { return this.http.delete(`/api/arcs/${id}`); } getArcDeletionImpact(id: string): Observable { return this.http.get(`/api/arcs/${id}/deletion-impact`); } /** Réordonne les arcs d'une campagne (glisser-déposer) : order = position. */ reorderArcs(orderedIds: string[]): Observable { return this.http.put('/api/arcs/reorder', { orderedIds }); } // ========== CHAPTER ========== /** Liste les chapitres d'un arc (donnée de scénario pure). */ getChapters(arcId: string): Observable { const params = new HttpParams().set('arcId', arcId); return this.http.get('/api/chapters', { params }); } getChapterById(id: string): Observable { return this.http.get(`/api/chapters/${id}`); } createChapter(payload: ChapterCreate): Observable { return this.http.post('/api/chapters', payload); } updateChapter(id: string, payload: ChapterCreate): Observable { return this.http.put(`/api/chapters/${id}`, payload); } deleteChapter(id: string): Observable { return this.http.delete(`/api/chapters/${id}`); } getChapterDeletionImpact(id: string): Observable { return this.http.get(`/api/chapters/${id}/deletion-impact`); } /** Réordonne (et déplace) les chapitres d'un arc : order = position, arcId = arc cible. */ reorderChapters(arcId: string, orderedIds: string[]): Observable { return this.http.put('/api/chapters/reorder', { arcId, orderedIds }); } // ========== SCENE ========== getScenes(chapterId: string): Observable { const params = new HttpParams().set('chapterId', chapterId); return this.http.get('/api/scenes', { params }); } getSceneById(id: string): Observable { return this.http.get(`/api/scenes/${id}`); } createScene(payload: SceneCreate): Observable { return this.http.post('/api/scenes', payload); } updateScene(id: string, payload: SceneCreate): Observable { return this.http.put(`/api/scenes/${id}`, payload); } deleteScene(id: string): Observable { return this.http.delete(`/api/scenes/${id}`); } /** Réordonne (et déplace) les scènes d'un chapitre : order = position, chapterId = cible. */ reorderScenes(chapterId: string, orderedIds: string[]): Observable { return this.http.put('/api/scenes/reorder', { chapterId, orderedIds }); } search(q: string): Observable { const params = new HttpParams().set('q', q); return this.http.get(`${this.apiUrl}/search`, { params }); } }