package com.foxx.androidcast.stats.ui;

/*********************************************************************
 * SessionStatsParser.java
 * Created at: Tue 19 May 2026 19:43:06 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class SessionStatsParser
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 29 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	0ce567cbbef716b35d3edd48e828089da7b4fffe0ca762e9b18f9dc7e7360fab
**********************************************************************/
import org.json.JSONObject;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/** Loads {@link SessionRecord} from on-device session JSON files. */
public final class SessionStatsParser {
    private SessionStatsParser() {}

    public static SessionRecord load(File file) throws Exception {
        if (file == null || !file.isFile()) {
            throw new IllegalArgumentException("Not a file");
        }
        byte[] bytes = Files.readAllBytes(file.toPath());
        String text = new String(bytes, StandardCharsets.UTF_8);
        JSONObject root = new JSONObject(text);
        return new SessionRecord(file.getName(), root);
    }
}
