import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import Character, {CharacterListResponse} from '../database/models/Character.js';
import type { CharacterPropsPost, CharacterAttribute } from '../database/models/Character.js';

interface AddCharacterData {
    character: CharacterPropsPost;
    bookId: string;
    id?: string;
}

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

// GET /character/list - Get character list
interface GetCharacterListData {
    bookid: string;
}
ipcMain.handle('db:character:list', createHandler<GetCharacterListData, CharacterListResponse>(
        function(userId: string, data: GetCharacterListData, lang: 'fr' | 'en'): CharacterListResponse {
            return Character.getCharacterList(userId, data.bookid, lang);
        }
    )
);

// GET /character/attribute - Get character attributes
interface GetCharacterAttributesData {
    characterId: string;
}
ipcMain.handle('db:character:attributes', createHandler<GetCharacterAttributesData, CharacterAttribute[]>(
        function(userId: string, data: GetCharacterAttributesData, lang: 'fr' | 'en'): CharacterAttribute[] {
            return Character.getAttributes(data.characterId, userId, lang);
        }
    )
);

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

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

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

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

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