const { app, BrowserWindow, screen, ipcMain } = require('electron');
const path = require('path');

function createWindow() {
    const { width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize;

    const win = new BrowserWindow({
        width: 350,
        height: 480,
        frame: false,
        transparent: true,
        alwaysOnTop: true,
        resizable: true,
        icon: path.join(__dirname, 'icon.png'),
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false
        }
    });

    // Default position: top right
    win.setPosition(screenWidth - 380, 50);

    win.loadFile('index.html');

    // Ensure it stays on top
    win.setAlwaysOnTop(true, 'screen-saver');

    // IPC handlers for window controls
    ipcMain.on('minimize-window', () => {
        win.minimize();
    });

    ipcMain.on('close-window', () => {
        win.close();
    });
}

app.whenReady().then(() => {
    createWindow();

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    });
});

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});
