package com.foxx.androidcast.vpn;

import android.content.Context;

import com.foxx.androidcast.BuildConfig;
import com.foxx.androidcast.crash.CrashReporter;
import com.foxx.androidcast.crash.CrashWatcherService;

/**
 * Records synthetic Java crashes from the VPN process for lab validation (debug builds only).
 * Does not kill the process — exercises persist + watcher upload path.
 */
public final class VpnCrashSimulator {
    private VpnCrashSimulator() {}

    public static boolean recordSimulatedCrash(Context context, int sequence) {
        if (!BuildConfig.DEBUG) {
            return false;
        }
        RuntimeException ex = new RuntimeException(
                "VpnCrashSimulation seq=" + sequence + " proc=" + CrashReporter.currentProcessName());
        ex.setStackTrace(buildStack(sequence));
        CrashReporter.recordJavaCrash(context, Thread.currentThread(), ex);
        CrashWatcherService.requestImmediateSweep(context.getApplicationContext(), true);
        return true;
    }

    private static StackTraceElement[] buildStack(int sequence) {
        return new StackTraceElement[]{
                new StackTraceElement("com.foxx.androidcast.vpn.WireGuardVpnEngine", "apply",
                        "WireGuardVpnEngine.java", 40 + (sequence % 5)),
                new StackTraceElement("com.foxx.androidcast.vpn.AndroidCastVpnService$1", "applyWireGuardConfig",
                        "AndroidCastVpnService.java", 60),
                new StackTraceElement("com.foxx.androidcast.vpn.AndroidCastVpnService", "onStartCommand",
                        "AndroidCastVpnService.java", 95 + (sequence % 3)),
                new StackTraceElement("android.app.ActivityThread", "handleServiceArgs",
                        "ActivityThread.java", 5001),
        };
    }
}
