import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import SeriesSpell, { SeriesSpellListResponse, SeriesSpellDetailProps } from '../database/models/SeriesSpell.js';

interface GetSpellListData {
    seriesId: string;
}

interface GetSpellDetailData {
    spellId: string;
}

interface AddSpellData {
    seriesId: string;
    name: string;
    description?: string | null;
    appearance?: string | null;
    tags?: string[];
    powerLevel?: string | null;
    components?: string | null;
    limitations?: string | null;
    notes?: string | null;
}

interface UpdateSpellData {
    id: string;
    name: string;
    description?: string | null;
    appearance?: string | null;
    tags?: string[];
    powerLevel?: string | null;
    components?: string | null;
    limitations?: string | null;
    notes?: string | null;
}

interface DeleteSpellData {
    spellId: string;
    deletedAt: number;
}

interface AddTagData {
    seriesId: string;
    name: string;
    color?: string | null;
}

interface UpdateTagData {
    tagId: string;
    name: string;
    color?: string | null;
}

interface DeleteTagData {
    tagId: string;
    deletedAt: number;
}

// GET /series/spell/list - Get spell list
ipcMain.handle('db:series:spell:list', createHandler<GetSpellListData, SeriesSpellListResponse>(
    function(userId: string, data: GetSpellListData, lang: 'fr' | 'en'): SeriesSpellListResponse {
        return SeriesSpell.getSpellList(userId, data.seriesId, lang);
    }
));

// GET /series/spell/detail - Get spell detail
ipcMain.handle('db:series:spell:detail', createHandler<GetSpellDetailData, SeriesSpellDetailProps>(
    function(userId: string, data: GetSpellDetailData, lang: 'fr' | 'en'): SeriesSpellDetailProps {
        return SeriesSpell.getSpellDetail(userId, data.spellId, lang);
    }
));

// POST /series/spell/add - Add spell
ipcMain.handle('db:series:spell:add', createHandler<AddSpellData, string>(
    function(userId: string, data: AddSpellData, lang: 'fr' | 'en'): string {
        return SeriesSpell.addSpell(userId, data.seriesId, data.name, lang, data.description, data.appearance, data.tags, data.powerLevel, data.components, data.limitations, data.notes);
    }
));

// PUT /series/spell/update - Update spell
ipcMain.handle('db:series:spell:update', createHandler<UpdateSpellData, boolean>(
    function(userId: string, data: UpdateSpellData, lang: 'fr' | 'en'): boolean {
        return SeriesSpell.updateSpell(userId, data.id, data.name, lang, data.description, data.appearance, data.tags, data.powerLevel, data.components, data.limitations, data.notes);
    }
));

// DELETE /series/spell/delete - Delete spell
ipcMain.handle('db:series:spell:delete', createHandler<DeleteSpellData, boolean>(
    function(userId: string, data: DeleteSpellData, lang: 'fr' | 'en'): boolean {
        return SeriesSpell.deleteSpell(userId, data.spellId, data.deletedAt, lang);
    }
));

// POST /series/spell/tag/add - Add tag
ipcMain.handle('db:series:spell:tag:add', createHandler<AddTagData, string>(
    function(userId: string, data: AddTagData, lang: 'fr' | 'en'): string {
        return SeriesSpell.addTag(userId, data.seriesId, data.name, lang, data.color);
    }
));

// PUT /series/spell/tag/update - Update tag
ipcMain.handle('db:series:spell:tag:update', createHandler<UpdateTagData, boolean>(
    function(userId: string, data: UpdateTagData, lang: 'fr' | 'en'): boolean {
        return SeriesSpell.updateTag(userId, data.tagId, data.name, lang, data.color);
    }
));

// DELETE /series/spell/tag/delete - Delete tag
ipcMain.handle('db:series:spell:tag:delete', createHandler<DeleteTagData, boolean>(
    function(userId: string, data: DeleteTagData, lang: 'fr' | 'en'): boolean {
        return SeriesSpell.deleteTag(userId, data.tagId, data.deletedAt, lang);
    }
));
