import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import Location, {LocationListResponse} from '../database/models/Location.js';
import type { LocationProps } from '../database/models/Location.js';

interface UpdateLocationResponse {
    valid: boolean;
    message: string;
}

interface AddLocationSectionData {
    locationName: string;
    bookId: string;
    id?: string;
    seriesLocationId?: string | null;
}

interface AddLocationElementData {
    locationId: string;
    elementName: string;
    id?: string;
}

interface AddLocationSubElementData {
    elementId: string;
    subElementName: string;
    id?: string;
}

interface UpdateLocationData {
    locations: LocationProps[];
}

// GET /location/all - Get all locations
interface GetAllLocationsData {
    bookid: string;
}
ipcMain.handle('db:location:all', createHandler<GetAllLocationsData, LocationListResponse>(
        function(userId: string, data: GetAllLocationsData, lang: 'fr' | 'en'): LocationListResponse {
            return Location.getAllLocations(userId, data.bookid, lang);
        }
    )
);

// POST /location/section/add - Add location section
ipcMain.handle('db:location:section:add', createHandler<AddLocationSectionData, string>(
        function(userId: string, data: AddLocationSectionData, lang: 'fr' | 'en'): string {
            return Location.addLocationSection(userId, data.locationName, data.bookId, lang, data.id, data.seriesLocationId || null);
        }
    )
);

// POST /location/element/add - Add location element
ipcMain.handle('db:location:element:add', createHandler<AddLocationElementData, string>(
        function(userId: string, data: AddLocationElementData, lang: 'fr' | 'en'): string {
            return Location.addLocationElement(userId, data.locationId, data.elementName, lang, data.id);
        }
    )
);

// POST /location/sub-element/add - Add location sub-element
ipcMain.handle('db:location:subelement:add', createHandler<AddLocationSubElementData, string>(
        function(userId: string, data: AddLocationSubElementData, lang: 'fr' | 'en'): string {
            return Location.addLocationSubElement(userId, data.elementId, data.subElementName, lang, data.id);
        }
    )
);

// POST /location/update - Update location section
ipcMain.handle('db:location:update', createHandler<UpdateLocationData, UpdateLocationResponse>(
        function(userId: string, data: UpdateLocationData, lang: 'fr' | 'en'): UpdateLocationResponse {
            return Location.updateLocationSection(userId, data.locations, lang);
        }
    )
);

// POST /location/section/update - Update location section with series link
interface UpdateSectionWithSeriesLinkData {
    sectionId: string;
    sectionName?: string;
    seriesLocationId?: string | null;
}
ipcMain.handle('db:location:section:update', createHandler<UpdateSectionWithSeriesLinkData, boolean>(
        function(userId: string, data: UpdateSectionWithSeriesLinkData, lang: 'fr' | 'en'): boolean {
            return Location.updateSectionWithSeriesLink(userId, data.sectionId, data.sectionName, data.seriesLocationId, lang);
        }
    )
);

// DELETE /location/delete - Delete location section
interface DeleteLocationData {
    locationId: string;
    bookId: string;
    deletedAt: number;
}
ipcMain.handle('db:location:delete', createHandler<DeleteLocationData, boolean>(
        function(userId: string, data: DeleteLocationData, lang: 'fr' | 'en'): boolean {
            return Location.deleteLocationSection(userId, data.bookId, data.locationId, data.deletedAt, lang);
        }
    )
);

// DELETE /location/element/delete - Delete location element
interface DeleteLocationElementData {
    elementId: string;
    bookId: string;
    deletedAt: number;
}
ipcMain.handle('db:location:element:delete', createHandler<DeleteLocationElementData, boolean>(
        function(userId: string, data: DeleteLocationElementData, lang: 'fr' | 'en'): boolean {
            return Location.deleteLocationElement(userId, data.bookId, data.elementId, data.deletedAt, lang);
        }
    )
);

// DELETE /location/sub-element/delete - Delete location sub-element
interface DeleteLocationSubElementData {
    subElementId: string;
    bookId: string;
    deletedAt: number;
}
ipcMain.handle('db:location:subelement:delete', createHandler<DeleteLocationSubElementData, boolean>(
        function(userId: string, data: DeleteLocationSubElementData, lang: 'fr' | 'en'): boolean {
            return Location.deleteLocationSubElement(userId, data.bookId, data.subElementId, data.deletedAt, lang);
        }
    )
);
