import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import SeriesLocation, { SeriesLocationListProps } from '../database/models/SeriesLocation.js';

interface GetLocationListData {
    seriesId: string;
}

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

interface AddElementData {
    locationId: string;
    name: string;
    description?: string;
}

interface AddSubElementData {
    elementId: string;
    name: string;
    description?: string;
}

interface DeleteLocationData {
    locationId: string;
    deletedAt: number;
}

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

interface DeleteSubElementData {
    subElementId: string;
    deletedAt: number;
}

// GET /series/location/list - Get location list
ipcMain.handle('db:series:location:list', createHandler<GetLocationListData, SeriesLocationListProps[]>(
    function(userId: string, data: GetLocationListData, lang: 'fr' | 'en'): SeriesLocationListProps[] {
        return SeriesLocation.getLocationList(userId, data.seriesId, lang);
    }
));

// POST /series/location/section/add - Add location section
ipcMain.handle('db:series:location:section:add', createHandler<AddLocationSectionData, string>(
    function(userId: string, data: AddLocationSectionData, lang: 'fr' | 'en'): string {
        return SeriesLocation.addLocationSection(userId, data.seriesId, data.name, lang);
    }
));

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

// POST /series/location/sub-element/add - Add sub-element
ipcMain.handle('db:series:location:subelement:add', createHandler<AddSubElementData, string>(
    function(userId: string, data: AddSubElementData, lang: 'fr' | 'en'): string {
        return SeriesLocation.addSubElement(userId, data.elementId, data.name, lang, data.description);
    }
));

// DELETE /series/location/delete - Delete location
ipcMain.handle('db:series:location:delete', createHandler<DeleteLocationData, boolean>(
    function(userId: string, data: DeleteLocationData, lang: 'fr' | 'en'): boolean {
        return SeriesLocation.deleteLocation(userId, data.locationId, data.deletedAt, lang);
    }
));

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

// DELETE /series/location/sub-element/delete - Delete sub-element
ipcMain.handle('db:series:location:subelement:delete', createHandler<DeleteSubElementData, boolean>(
    function(userId: string, data: DeleteSubElementData, lang: 'fr' | 'en'): boolean {
        return SeriesLocation.deleteSubElement(userId, data.subElementId, data.deletedAt, lang);
    }
));
