import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import SeriesCharacter, { SeriesCharacterPropsPost, SeriesCharacterListProps, CharacterAttributesResponse } from '../database/models/SeriesCharacter.js';

interface GetCharacterListData {
    seriesId: string;
}

interface GetCharacterAttributesData {
    characterId: string;
}

interface AddCharacterData {
    seriesId: string;
    character: SeriesCharacterPropsPost;
}

interface UpdateCharacterData {
    character: SeriesCharacterPropsPost;
}

interface DeleteCharacterData {
    characterId: string;
    deletedAt: number;
}

interface AddAttributeData {
    characterId: string;
    type: string;
    name: string;
}

interface DeleteAttributeData {
    attributeId: string;
    deletedAt: number;
}

// GET /series/character/list - Get character list
ipcMain.handle('db:series:character:list', createHandler<GetCharacterListData, SeriesCharacterListProps[]>(
    function(userId: string, data: GetCharacterListData, lang: 'fr' | 'en'): SeriesCharacterListProps[] {
        return SeriesCharacter.getCharacterList(userId, data.seriesId, lang);
    }
));

// GET /series/character/attribute - Get character attributes
ipcMain.handle('db:series:character:attributes', createHandler<GetCharacterAttributesData, CharacterAttributesResponse>(
    function(userId: string, data: GetCharacterAttributesData, lang: 'fr' | 'en'): CharacterAttributesResponse {
        return SeriesCharacter.getCharacterAttributes(userId, data.characterId, lang);
    }
));

// POST /series/character/add - Add new character
ipcMain.handle('db:series:character:add', createHandler<AddCharacterData, string>(
    function(userId: string, data: AddCharacterData, lang: 'fr' | 'en'): string {
        return SeriesCharacter.addNewCharacter(userId, data.character, data.seriesId, lang);
    }
));

// PATCH /series/character/update - Update character
ipcMain.handle('db:series:character:update', createHandler<UpdateCharacterData, boolean>(
    function(userId: string, data: UpdateCharacterData, lang: 'fr' | 'en'): boolean {
        return SeriesCharacter.updateCharacter(userId, data.character, lang);
    }
));

// DELETE /series/character/delete - Delete character
ipcMain.handle('db:series:character:delete', createHandler<DeleteCharacterData, boolean>(
    function(userId: string, data: DeleteCharacterData, lang: 'fr' | 'en'): boolean {
        return SeriesCharacter.deleteCharacter(userId, data.characterId, data.deletedAt, lang);
    }
));

// POST /series/character/attribute/add - Add attribute
ipcMain.handle('db:series:character:attribute:add', createHandler<AddAttributeData, string>(
    function(userId: string, data: AddAttributeData, lang: 'fr' | 'en'): string {
        return SeriesCharacter.addNewAttribute(data.characterId, userId, data.type, data.name, lang);
    }
));

// DELETE /series/character/attribute/delete - Delete attribute
ipcMain.handle('db:series:character:attribute:delete', createHandler<DeleteAttributeData, boolean>(
    function(userId: string, data: DeleteAttributeData, lang: 'fr' | 'en'): boolean {
        return SeriesCharacter.deleteAttribute(userId, data.attributeId, data.deletedAt, lang);
    }
));
