const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('resolveAPI', {
    isWindows: process.platform === 'win32',

    // Queries active project name in DaVinci Resolve
    getProjectName: () => ipcRenderer.invoke('resolve:getProjectName'),
    
    downloadAsset: (firstArg, url, filename, customPath) => {
        if (typeof firstArg === 'object' && firstArg !== null) {
            // Object form: renderer passes all fields including customPath
            return ipcRenderer.invoke('assets:download', firstArg);
        }
        // Legacy 3/4-arg form
        return ipcRenderer.invoke('assets:download', { id: firstArg, url, filename, customPath: customPath || '' });
    },
    
    // Select custom download folder directory
    selectFolder: (currentPath) => ipcRenderer.invoke('dialog:select-folder', currentPath),
    
    // Open path in OS explorer
    openFolder: (pathUrl) => ipcRenderer.invoke('shell:open-path', pathUrl),
    
    // Event listener to track file downloads in real-time
    onDownloadProgress: (callback) => {
        const subscription = (event, value) => callback(value);
        ipcRenderer.on('download-progress', subscription);
        
        // Return unsubscribe function
        return () => {
            ipcRenderer.removeListener('download-progress', subscription);
        };
    },

    // Manually trigger media pool import
    importMedia: (filePath) => ipcRenderer.invoke('resolve:importMedia', filePath),

    // Dynamic Online Update Verification
    checkUpdate: () => ipcRenderer.invoke('app:check-update'),

    // Secure Disk Settings Persistence APIs (Port/Origin Independent)
    saveSettings: (settings) => ipcRenderer.invoke('settings:save', settings),
    loadSettings: () => ipcRenderer.invoke('settings:load'),

    // Subtitles and Transcription API
    exportTimelineAudio: (trackIndex) => ipcRenderer.invoke('resolve:exportTimelineAudio', { trackIndex }),
    transcribeAudio: (filePath, apiKey, model) => ipcRenderer.invoke('ai:transcribeAudio', { filePath, apiKey, model }),
    importFcpxmlSubtitles: (subtitlesJson, styleSettings) => ipcRenderer.invoke('resolve:importFcpxmlSubtitles', { subtitlesJson, styleSettings }),
    addTextPlusSubtitles: (subtitlesJson, styleSettings) => ipcRenderer.invoke('resolve:addTextPlusSubtitles', { subtitlesJson, styleSettings }),
    getGeminiModels: (apiKey) => ipcRenderer.invoke('ai:getGeminiModels', apiKey),
    getAudioTracks: () => ipcRenderer.invoke('resolve:getAudioTracks'),
    getTimelineDuration: () => ipcRenderer.invoke('resolve:getTimelineDuration'),

    // Cleans up Resolve scripting interfaces
    cleanup: () => ipcRenderer.invoke('resolve:cleanup'),

    // Open URL in default OS browser
    openExternal: (url) => ipcRenderer.invoke('shell:open-external', url),

    // Reactor Atoms Manager APIs
    getReactorAtoms: (forceSync) => ipcRenderer.invoke('reactor:getAtoms', { forceSync }),
    installReactorAtom: (fileUrl, deployPath) => ipcRenderer.invoke('reactor:installAtom', { fileUrl, deployPath }),
    onReactorCacheUpdated: (callback) => {
        const subscription = (event, value) => callback(value);
        ipcRenderer.on('reactor:cacheUpdated', subscription);
        return () => {
            ipcRenderer.removeListener('reactor:cacheUpdated', subscription);
        };
    },
    onUpdateInstalled: (callback) => {
        const subscription = (event, value) => callback(value);
        ipcRenderer.on('app:update-installed', subscription);
        return () => {
            ipcRenderer.removeListener('app:update-installed', subscription);
        };
    }
});
