package com.foxx.androidcast.dev;

import android.os.Build;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * In-memory diagnostics snapshot for developer settings.
 * {@link #toJson()} is ready for a future BE upload endpoint (not wired yet).
 */
public final class DevDiagnosticsReport {
    public final long collectedAtEpochMs;
    public final String deviceModel;
    public final int sdkInt;
    public final Map<String, String> sections;

    public DevDiagnosticsReport(long collectedAtEpochMs, Map<String, String> sections) {
        this.collectedAtEpochMs = collectedAtEpochMs;
        this.deviceModel = Build.MANUFACTURER + " " + Build.MODEL;
        this.sdkInt = Build.VERSION.SDK_INT;
        this.sections = new LinkedHashMap<>(sections);
    }

    public JSONObject toJson() {
        JSONObject root = new JSONObject();
        try {
            root.put("schema_version", 1);
            root.put("kind", "dev_diagnostics");
            root.put("collected_at_ms", collectedAtEpochMs);
            root.put("device_model", deviceModel);
            root.put("sdk_int", sdkInt);
            JSONObject body = new JSONObject();
            for (Map.Entry<String, String> e : sections.entrySet()) {
                body.put(e.getKey(), e.getValue());
            }
            root.put("sections", body);
            root.put("sections_array", sectionsToArray());
        } catch (Exception ignored) {
        }
        return root;
    }

    private JSONArray sectionsToArray() {
        JSONArray arr = new JSONArray();
        for (Map.Entry<String, String> e : sections.entrySet()) {
            JSONObject item = new JSONObject();
            try {
                item.put("id", e.getKey());
                item.put("text", e.getValue());
                arr.put(item);
            } catch (Exception ignored) {
            }
        }
        return arr;
    }
}
