import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import SeriesWorld, { SeriesWorldListProps, SeriesWorldUpdateProps } from '../database/models/SeriesWorld.js';

interface GetWorldListData {
    seriesId: string;
}

interface AddWorldData {
    seriesId: string;
    name: string;
}

interface UpdateWorldData {
    worldId: string;
    name: string;
    history?: string;
    politics?: string;
    economy?: string;
    religion?: string;
    languages?: string;
}

interface AddElementData {
    worldId: string;
    elementType: number;
    name: string;
    description?: string;
}

interface DeleteElementData {
    elementId: string;
    deletedAt: number;
}

// GET /series/world/list - Get world list
ipcMain.handle('db:series:world:list', createHandler<GetWorldListData, SeriesWorldListProps[]>(
    function(userId: string, data: GetWorldListData, lang: 'fr' | 'en'): SeriesWorldListProps[] {
        return SeriesWorld.getWorldList(userId, data.seriesId, lang);
    }
));

// POST /series/world/add - Add world
ipcMain.handle('db:series:world:add', createHandler<AddWorldData, string>(
    function(userId: string, data: AddWorldData, lang: 'fr' | 'en'): string {
        return SeriesWorld.addWorld(userId, data.seriesId, data.name, lang);
    }
));

// PATCH /series/world/update - Update world
ipcMain.handle('db:series:world:update', createHandler<UpdateWorldData, boolean>(
    function(userId: string, data: UpdateWorldData, lang: 'fr' | 'en'): boolean {
        const worldData: SeriesWorldUpdateProps = {
            name: data.name,
            history: data.history,
            politics: data.politics,
            economy: data.economy,
            religion: data.religion,
            languages: data.languages
        };
        return SeriesWorld.updateWorld(userId, data.worldId, worldData, lang);
    }
));

// POST /series/world/element/add - Add element
ipcMain.handle('db:series:world:element:add', createHandler<AddElementData, string>(
    function(userId: string, data: AddElementData, lang: 'fr' | 'en'): string {
        return SeriesWorld.addElement(userId, data.worldId, data.elementType, data.name, lang, data.description);
    }
));

// DELETE /series/world/element/delete - Delete element
ipcMain.handle('db:series:world:element:delete', createHandler<DeleteElementData, boolean>(
    function(userId: string, data: DeleteElementData, lang: 'fr' | 'en'): boolean {
        return SeriesWorld.deleteElement(userId, data.elementId, data.deletedAt, lang);
    }
));
