package com.foxx.androidcast.crash;

/*********************************************************************
 * CrashSettingsStore.java
 * Created at: Wed 20 May 2026 14:31:55 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class CrashSettingsStore
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 86 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	ac2e9d6760032dddd3a23524eda1c77f4424a11348d758e4f6592001d2d72fe6
**********************************************************************/
import android.content.Context;

import com.foxx.androidcast.AppPreferences;
import com.foxx.androidcast.network.BackendEndpoints;
import com.foxx.androidcast.settings.AppSettingsJson;

import org.json.JSONObject;

/** Loads effective crash settings (preferences + settings.json overrides). */
public final class CrashSettingsStore {
    private static final long DEFAULT_UPLOAD_INTERVAL_MS = 5 * 60_000L;
    private static final int DEFAULT_MAX_PENDING = 32;

    private CrashSettingsStore() {}

    public static CrashSettings load(Context context) {
        boolean enabled = AppPreferences.isSendAnonymousCrashLogs(context);
        String uploadUrl = CrashSettings.DEFAULT_UPLOAD_URL;
        boolean prune = true;
        long interval = DEFAULT_UPLOAD_INTERVAL_MS;
        int maxPending = DEFAULT_MAX_PENDING;

        JSONObject crash = AppSettingsJson.section(context, "crash");
        if (crash != null) {
            if (crash.has("enabled")) {
                enabled = parseBool(crash.optString("enabled", "true"), enabled);
            }
            String url = crash.optString("upload_url", "").trim();
            if (!url.isEmpty()) {
                uploadUrl = BackendEndpoints.normalizeCrashesUrl(url);
            }
            if (crash.has("prune_after_upload")) {
                prune = parseBool(crash.optString("prune_after_upload", "true"), prune);
            }
            String intervalRaw = crash.optString("upload_interval", "");
            if (!intervalRaw.isEmpty()) {
                interval = parseDurationMs(intervalRaw, interval);
            }
            maxPending = crash.optInt("max_pending", maxPending);
        }
        return new CrashSettings(enabled, uploadUrl, prune, interval, maxPending);
    }

    private static boolean parseBool(String raw, boolean fallback) {
        if (raw == null) {
            return fallback;
        }
        String v = raw.trim().toLowerCase();
        if ("true".equals(v) || "1".equals(v) || "yes".equals(v)) {
            return true;
        }
        if ("false".equals(v) || "0".equals(v) || "no".equals(v)) {
            return false;
        }
        return fallback;
    }

    private static long parseDurationMs(String raw, long fallback) {
        try {
            String t = raw.trim().toLowerCase();
            if (t.endsWith("ms")) {
                return Long.parseLong(t.substring(0, t.length() - 2).trim());
            }
            if (t.endsWith("s")) {
                return Long.parseLong(t.substring(0, t.length() - 1).trim()) * 1000L;
            }
            if (t.endsWith("m")) {
                return Long.parseLong(t.substring(0, t.length() - 1).trim()) * 60_000L;
            }
            if (t.endsWith("h")) {
                return Long.parseLong(t.substring(0, t.length() - 1).trim()) * 3_600_000L;
            }
            return Long.parseLong(t);
        } catch (Exception e) {
            return fallback;
        }
    }
}
