import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import RemovedItemsRepository, { RemovedItemRecord } from '../database/repositories/removed-items.repository.js';
import Book from '../database/models/Book.js';
import Chapter from '../database/models/Chapter.js';
import Character from '../database/models/Character.js';
import Location from '../database/models/Location.js';
import World from '../database/models/World.js';
import Incident from '../database/models/Incident.js';
import PlotPoint from '../database/models/PlotPoint.js';
import Issue from '../database/models/Issue.js';
import Spell from '../database/models/Spell.js';
import Series from '../database/models/Series.js';
import SeriesCharacter from '../database/models/SeriesCharacter.js';
import SeriesLocation from '../database/models/SeriesLocation.js';
import SeriesWorld from '../database/models/SeriesWorld.js';
import SeriesSpell from '../database/models/SeriesSpell.js';

/**
 * Get tombstones since a specific timestamp.
 */
ipcMain.handle('db:tombstones:since', createHandler<number, RemovedItemRecord[]>(
    function(userId: string, since: number, lang: 'fr' | 'en'): RemovedItemRecord[] {
        return RemovedItemsRepository.getDeletionsSince(userId, since, lang);
    })
);

/**
 * Apply server tombstones for book entities locally.
 */
ipcMain.handle('db:tombstones:apply:books', createHandler<RemovedItemRecord[], void>(
    function(userId: string, tombstones: RemovedItemRecord[], lang: 'fr' | 'en'): void {
        for (const tombstone of tombstones) {
            switch (tombstone.table_name) {
                case 'erit_books':
                    Book.removeBook(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_chapters':
                    Chapter.removeChapter(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_chapter_infos':
                    Chapter.removeChapterInformation(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_characters':
                    Character.deleteCharacter(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_characters_attributes':
                    Character.deleteAttribute(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_location':
                    Location.deleteLocationSection(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'location_element':
                    Location.deleteLocationElement(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'location_sub_element':
                    Location.deleteLocationSubElement(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_world_elements':
                    World.removeElementFromWorld(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_incidents':
                    Incident.removeIncident(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_plot_points':
                    PlotPoint.removePlotPoint(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_issues':
                    Issue.removeIssue(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_spells':
                    Spell.deleteSpell(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'book_spell_tags':
                    Spell.deleteSpellTag(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
            }
        }
    })
);

/**
 * Apply server tombstones for series entities locally.
 */
ipcMain.handle('db:tombstones:apply:series', createHandler<RemovedItemRecord[], void>(
    function(userId: string, tombstones: RemovedItemRecord[], lang: 'fr' | 'en'): void {
        for (const tombstone of tombstones) {
            switch (tombstone.table_name) {
                case 'erit_series':
                    Series.deleteSeries(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_books':
                    Series.removeBookFromSeries(userId, tombstone.book_id!, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_characters':
                    SeriesCharacter.deleteCharacter(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_characters_attributes':
                    SeriesCharacter.deleteAttribute(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_locations':
                    SeriesLocation.deleteLocation(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_location_elements':
                    SeriesLocation.deleteElement(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_location_sub_elements':
                    SeriesLocation.deleteSubElement(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_world_elements':
                    SeriesWorld.deleteElement(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_spells':
                    SeriesSpell.deleteSpell(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
                case 'series_spell_tags':
                    SeriesSpell.deleteTag(userId, tombstone.entity_id, tombstone.deleted_at, lang);
                    break;
            }
        }
    })
);
