• Exports all statistics data to JSON

    Creates a comprehensive JSON export containing the complete dataset of all statistics, metrics, and analyzed patterns. This is the most complete export option, providing structured data that preserves all relationships between different statistical elements.

    The export includes:

    • Complete statistics data structure with all metrics
    • Full skipped tracks history with detailed event data
    • Artist-level metrics and analysis
    • Library-wide statistics and aggregations
    • Temporal patterns and behavioral insights
    • Export metadata (timestamp, application version)

    This function is ideal for:

    • Full system backups
    • Data migration between devices
    • External analysis in specialized tools
    • Debugging and troubleshooting

    Parameters

    • mainWindow: BrowserWindow

      The Electron BrowserWindow to attach dialogs to

    • OptionaltargetPath: string

      Optional pre-defined export path (bypasses user prompt)

    Returns Promise<{ success: boolean; message?: string; filePath?: string }>

    Promise resolving to object containing success status, message, and path

    // Export all data with user-selected path
    const result = await exportAllStatisticsToJSON(mainWindow);
    if (result.success) {
    showNotification(`Full backup saved to ${result.filePath}`);
    }
    export async function exportAllStatisticsToJSON(
    mainWindow: BrowserWindow,
    targetPath?: string,
    ): Promise<{ success: boolean; message?: string; filePath?: string }> {
    try {
    // Get all statistics data
    const statistics = await getStatistics();
    const skippedTracks = await getSkippedTracks();
    const artistMetrics = await aggregateArtistSkipMetrics();
    const libraryStats = await calculateLibrarySkipStatistics();
    const timePatterns = await analyzeTimeBasedPatterns();

    // Combine all data
    const exportData = {
    statistics,
    skippedTracks,
    artistMetrics,
    libraryStats,
    timePatterns,
    exportDate: new Date().toISOString(),
    version: app.getVersion(),
    };

    // Determine file path
    let filePath = targetPath;
    if (!filePath) {
    const timestamp = new Date()
    .toISOString()
    .replace(/:/g, "-")
    .split(".")[0];
    const defaultFileName = `spotify_skip_tracker_data_${timestamp}.json`;

    filePath = await promptForExportLocation(
    mainWindow,
    join(ensureExportDir(), defaultFileName),
    [{ name: "JSON Files", extensions: ["json"] }],
    );

    if (!filePath) {
    return { success: false, message: "Export was canceled" };
    }
    }

    // Write the JSON file
    writeFileSync(filePath, JSON.stringify(exportData, null, 2));

    return {
    success: true,
    message: "All statistics data exported successfully",
    filePath,
    };
    } catch (error) {
    console.error("Error exporting statistics to JSON:", error);
    return {
    success: false,
    message: `Error exporting data: ${error instanceof Error ? error.message : "Unknown error"}`,
    };
    }
    }