// Universal ModEvent sending that works across SKSE versions
// Replace the ModEvent section in CaptureWorkerThread with this:

// Send ModEvent through task interface to ensure it runs on main thread
auto* taskInterface = SKSE::GetTaskInterface();
if (taskInterface) {
    taskInterface->AddTask([result = g_result]() {
        try {
            // Method 1: Try newer SKSE API (2.2+)
            auto* eventSource = RE::ScriptEventSourceHolder::GetSingleton();
            if (eventSource) {
                #if SKSE_VERSION_MINOR >= 2
                // For SKSE 2.2+
                auto* modEventSource = eventSource->GetEventSource<RE::TESModEvent>();
                if (modEventSource) {
                    auto modEvent = RE::TESModEvent::Create();
                    if (modEvent) {
                        modEvent->eventName = "PrintScreenComplete";
                        modEvent->strArg = result.c_str();
                        modEvent->numArg = 0.0f;
                        modEvent->sender = nullptr;
                        modEventSource->SendEvent(modEvent);
                        logger::info("ModEvent sent (SKSE 2.2+): PrintScreenComplete");
                        return;
                    }
                }
                #endif
                
                // Method 2: Try older SKSE API (fallback)
                try {
                    // This works with older SKSE versions
                    RE::BSFixedString eventName = "PrintScreenComplete";
                    RE::BSFixedString strArg = result.c_str();
                    float numArg = 0.0f;
                    RE::TESForm* sender = nullptr;
                    
                    // Try direct call (works in some versions)
                    auto vm = RE::BSScript::Internal::VirtualMachine::GetSingleton();
                    if (vm) {
                        vm->SendEvent(nullptr, eventName, strArg, numArg, sender);
                        logger::info("ModEvent sent (older SKSE): PrintScreenComplete");
                        return;
                    }
                } catch (...) {
                    // Ignore and try next method
                }
                
                // Method 3: Console command fallback (most compatible)
                try {
                    auto* console = RE::ConsoleLog::GetSingleton();
                    std::string command = fmt::format("SendModEvent \"PrintScreenComplete\" \"{}\" 0.0", result);
                    
                    // Execute via script
                    auto* scriptFactory = RE::IFormFactory::GetConcreteFormFactoryByType<RE::Script>();
                    if (scriptFactory) {
                        auto script = scriptFactory->Create();
                        if (script) {
                            script->SetCommand(command);
                            script->CompileAndRun(nullptr);
                            logger::info("ModEvent sent (console): PrintScreenComplete");
                            return;
                        }
                    }
                } catch (...) {
                    // Final fallback failed
                }
            }
            
            logger::error("All ModEvent methods failed");
            
        } catch (const std::exception& e) {
            logger::error("Exception in ModEvent sending: {}", e.what());
        } catch (...) {
            logger::error("Unknown exception in ModEvent sending");
        }
    });
} else {
    logger::error("Failed to get TaskInterface");
}