// Isolated behavioral harness for SettingsBackup's live restore handoff.
// Every external consumer is replaced before restore output is exercised, so
// this file never writes the real compositor, wallpaper, keymap, or shell.
import Quickshell
import Quickshell.Io
import QtQuick

import qs.services

ShellRoot {
    id: root

    property var calls: []
    property bool homeInitialized: false
    property var homeFavorites: []
    property bool displayOperationBusy: false
    property bool displayBlocked: false
    property var displayGeneration: ({ "DP-2": { mode: "4500x3000@60", scale: 1.5, transform: 0 } })

    function record(name: string): void {
        const next = root.calls.slice();
        next.push(name);
        root.calls = next;
    }

    Component.onCompleted: {
        SettingsBackup.readHomeState = function() {
            return {
                initialized: root.homeInitialized,
                favorites: root.homeFavorites
            };
        };
        SettingsBackup.resetHome = function() {
            root.record("home.reset");
            root.homeInitialized = false;
            root.homeFavorites = [];
        };
        SettingsBackup.initializeHome = function(ids) {
            root.record("home.initialize:" + ids.join(","));
            root.homeInitialized = true;
            root.homeFavorites = ids.map(id => ({ id: id, alias: "" }));
        };
        SettingsBackup.aliasHome = function(id, alias) {
            root.record("home.alias:" + id + "=" + alias);
            root.homeFavorites = root.homeFavorites.map(favorite =>
                favorite.id === id ? { id: id, alias: alias } : favorite);
        };
        SettingsBackup.reloadDesktop = function() { root.record("desktop.reload"); };
        SettingsBackup.readDisplays = function() { return root.displayGeneration; };
        SettingsBackup.protectDisplays = function(value) {
            root.record("display.protect:" + JSON.stringify(value));
            root.displayGeneration = value;
            return true;
        };
        SettingsBackup.displayBusy = function() { return root.displayOperationBusy; };
        SettingsBackup.setDisplayBlocked = function(blocked) {
            root.record("display.block:" + blocked);
            root.displayBlocked = blocked;
        };
        SettingsBackup.applyCompositor = function() { root.record("system.apply"); };
        SettingsBackup.reloadKeybinds = function() { root.record("keybinds.reload"); };
        SettingsBackup.keybindsReloading = function() { return false; };
        SettingsBackup.systemBusy = function() { return false; };
        SettingsBackup.currentWallpaper = function() { return "/tmp/restored-wallpaper.jpg"; };
        SettingsBackup.applyWallpaper = function(path) { root.record("wallpaper.set:" + path); };
        SettingsBackup.reloadShell = function() { root.record("shell.reload"); };
    }

    IpcHandler {
        target: "settings-backup-behavior"

        function reset(): void {
            root.calls = [];
            root.homeInitialized = false;
            root.homeFavorites = [];
            root.displayOperationBusy = false;
            root.displayBlocked = false;
            SettingsBackup.protectedDisplays = root.displayGeneration;
        }

        function apply(output: string): bool {
            return SettingsBackup.handleRestoreOutput(output);
        }

        function restoreWhileDisplayBusy(): bool {
            root.displayOperationBusy = true;
            SettingsBackup.snapshots = [{ name: "settings-20260818-010203004.json" }];
            return SettingsBackup.restore("settings-20260818-010203004.json");
        }

        function status(): string {
            return JSON.stringify({
                calls: root.calls,
                initialized: root.homeInitialized,
                favorites: root.homeFavorites,
                displayBlocked: root.displayBlocked,
                displays: root.displayGeneration,
                lastError: SettingsBackup.lastError
            });
        }
    }
}
