import { ipcMain } from 'electron'; import { createHandler } from '../database/LocalSystem.js'; import Book from '../database/models/Book.js'; import type { BookProps, GuideLine, GuideLineAI, Act, Issue, WorldProps } from '../database/models/Book.js'; interface UpdateBookBasicData { title: string; subTitle: string; summary: string; publicationDate: string; wordCount: number; bookId: string; } interface UpdateGuideLineData { bookId: string; tone: string | null; atmosphere: string | null; writingStyle: string | null; themes: string | null; symbolism: string | null; motifs: string | null; narrativeVoice: string | null; pacing: string | null; keyMessages: string | null; intendedAudience: string | null; } interface StoryData { acts: Act[]; issues: Issue[]; } interface UpdateStoryData { bookId: string; acts: Act[]; mainChapters: any[]; // ChapterProps[] from your API } interface CreateBookData { title: string; subTitle: string | null; summary: string | null; type: string; serieId: number | null; desiredReleaseDate: string | null; desiredWordCount: number | null; } interface AddIncidentData { bookId: string; name: string; } interface AddPlotPointData { bookId: string; name: string; incidentId: string; } interface AddIssueData { bookId: string; name: string; } interface AddWorldData { bookId: string; worldName: string; } interface AddWorldElementData { worldId: string; elementName: string; elementType: number; } interface SetAIGuideLineData { bookId: string; narrativeType: number; dialogueType: number; plotSummary: string; toneAtmosphere: string; verbTense: number; language: number; themes: string; } // GET /books - Get all books ipcMain.handle('db:book:books', createHandler( async function(userId: string, _body: void, lang: 'fr' | 'en'):Promise { return await Book.getBooks(userId, lang); } ) ); // GET /book/:id - Get single book ipcMain.handle('db:book:bookBasicInformation', createHandler( async function(userId: string, bookId: string, lang: 'fr' | 'en'):Promise { return await Book.getBook(bookId, userId); } ) ); // POST /book/basic-information - Update book basic info ipcMain.handle('db:book:updateBasicInformation', createHandler( function(userId: string, data: UpdateBookBasicData, lang: 'fr' | 'en') { return Book.updateBookBasicInformation(userId, data.title, data.subTitle, data.summary, data.publicationDate, data.wordCount, data.bookId, lang); } ) ); // GET /book/guide-line - Get guideline ipcMain.handle( 'db:book:guideline:get', createHandler(async function(userId: string, bookId: string, lang: 'fr' | 'en') { return await Book.getGuideLine(userId, bookId, lang); } ) ); // POST /book/guide-line - Update guideline ipcMain.handle('db:book:guideline:update', createHandler( async function(userId: string, data: UpdateGuideLineData, lang: 'fr' | 'en') { return await Book.updateGuideLine( userId, data.bookId, data.tone, data.atmosphere, data.writingStyle, data.themes, data.symbolism, data.motifs, data.narrativeVoice, data.pacing, data.keyMessages, data.intendedAudience, lang ); } ) ); // GET /book/story - Get story data (acts + issues) ipcMain.handle('db:book:story:get', createHandler( async function(userId: string, bookId: string, lang: 'fr' | 'en'):Promise { const acts:Act[] = await Book.getActsData(userId, bookId, lang); const issues:Issue[] = await Book.getIssuesFromBook(userId, bookId, lang); return { acts, issues }; } ) ); // POST /book/story - Update story (acts + mainChapters) ipcMain.handle('db:book:story:update', createHandler( function(userId: string, data: UpdateStoryData, lang: 'fr' | 'en'):boolean { return Book.updateStory(userId, data.bookId, data.acts, data.mainChapters, lang); } ) ); // POST /book/add - Create new book ipcMain.handle('db:book:create', createHandler( function(userId: string, data: CreateBookData, lang: 'fr' | 'en') { return Book.addBook( null, userId, data.title, data.subTitle || '', data.summary || '', data.type, data.serieId || 0, data.desiredReleaseDate || '', data.desiredWordCount || 0, lang ); } ) ); // POST /book/incident/new - Add incident ipcMain.handle( 'db:book:incident:add', createHandler( function(userId: string, data: AddIncidentData, lang: 'fr' | 'en') { return Book.addNewIncident(userId, data.bookId, data.name, lang); } ) ); // DELETE /book/incident/remove - Remove incident interface RemoveIncidentData { bookId: string; incidentId: string; } ipcMain.handle('db:book:incident:remove', createHandler( function(userId: string, data: RemoveIncidentData, lang: 'fr' | 'en') { return Book.removeIncident(userId, data.bookId, data.incidentId, lang); } ) ); // POST /book/plot/new - Add plot point ipcMain.handle('db:book:plot:add', createHandler( function(userId: string, data: AddPlotPointData, lang: 'fr' | 'en') { return Book.addNewPlotPoint( userId, data.bookId, data.incidentId, data.name, lang ); } ) ); // DELETE /book/plot/remove - Remove plot point ipcMain.handle( 'db:book:plot:remove', createHandler( function(userId: string, plotPointId: string, lang: 'fr' | 'en') { return Book.removePlotPoint(userId, plotPointId, lang); } ) ); // POST /book/issue/add - Add issue ipcMain.handle('db:book:issue:add', createHandler( function(userId: string, data: AddIssueData, lang: 'fr' | 'en') { return Book.addNewIssue(userId, data.bookId, data.name, lang); } ) ); // DELETE /book/issue/remove - Remove issue ipcMain.handle('db:book:issue:remove', createHandler( function(userId: string, issueId: string, lang: 'fr' | 'en') { return Book.removeIssue(userId, issueId, lang); } ) ); // GET /book/worlds - Get worlds for book ipcMain.handle('db:book:worlds:get', createHandler( function(userId: string, bookId: string, lang: 'fr' | 'en') { return Book.getWorlds(userId, bookId, lang); } ) ); // POST /book/world/add - Add world ipcMain.handle('db:book:world:add', createHandler( function(userId: string, data: AddWorldData, lang: 'fr' | 'en') { return Book.addNewWorld(userId, data.bookId, data.worldName, lang); } ) ); // POST /book/world/element/add - Add element to world ipcMain.handle('db:book:world:element:add', createHandler( function(userId: string, data: AddWorldElementData, lang: 'fr' | 'en') { return Book.addNewElementToWorld( userId, data.worldId, data.elementName, data.elementType.toString(), lang ); } ) ); // DELETE /book/world/element/delete - Remove element from world ipcMain.handle('db:book:world:element:remove', createHandler( function(userId: string, elementId: string, lang: 'fr' | 'en') { return Book.removeElementFromWorld(userId, elementId, lang); } ) ); // DELETE /book/delete - Delete book ipcMain.handle('db:book:delete', createHandler( function(userId: string, bookId: string, lang: 'fr' | 'en') { return Book.removeBook(userId, bookId, lang); } ) ); // GET /book/ai/guideline - Get AI guideline ipcMain.handle('db:book:guideline:ai:get', createHandler( function(userId: string, bookId: string, lang: 'fr' | 'en') { return Book.getGuideLineAI(userId, bookId, lang); } ) ); // POST /book/ai/guideline (set) - Set AI guideline ipcMain.handle('db:book:guideline:ai:set', createHandler( function(userId: string, data: SetAIGuideLineData, lang: 'fr' | 'en') { return Book.setAIGuideLine( userId, data.bookId, data.narrativeType, data.dialogueType, data.plotSummary, data.toneAtmosphere, data.verbTense, data.language, data.themes, lang ); } ) ); // PUT /book/world/update - Update world interface UpdateWorldData { bookId: string; world: WorldProps; } ipcMain.handle('db:book:world:update', createHandler( function(userId: string, data: UpdateWorldData, lang: 'fr' | 'en') { return Book.updateWorld(userId, data.world, lang); } ) );