namespace QuickSetup {
    public class ExplorePage : QsPage {

        private Adw.StatusPage status_page;
        public override string page_id { get { return "explore"; } }
        public override bool can_go_back_to { get { return true; } }

        public ExplorePage(PageContext context) {
            base(context, "Explore stillOS");

            // Content
            this.status_page = new Adw.StatusPage();
            this.status_page.title = tr("You're All Set!");
            this.status_page.description = build_description(context.window.driver_reboot_required);
            this.status_page.icon_name = "io.stillhq.explore";
            this.status_page.vexpand = true;
            this.status_page.hexpand = true;
            this.status_page.valign = Gtk.Align.CENTER;
            this.status_page.halign = Gtk.Align.FILL;
            this.add_content(status_page);

            // Button box for two options (side by side)
            var button_box = this.create_button_bar();
            button_box.valign = Gtk.Align.CENTER;

            // Exit button
            var exit_button = this.create_page_button("Exit");
            exit_button.clicked.connect(() => {
                complete_setup();
                context.window.allow_close = true;
                context.window.close();
            });
            button_box.append(exit_button);

            // Explore button (recommended)
            var explore_button = this.create_page_button("Exit & Open stillExplore", true);
            explore_button.clicked.connect(() => {
                open_still_explore();
                complete_setup();
                context.window.allow_close = true;
                context.window.close();
            });
            button_box.append(explore_button);

            this.status_page.set_child(button_box);
        }

        private string build_description(bool driver_reboot_required) {
            string description = tr("Would you like to explore what stillOS has to offer?\n\nWe recommend stillExplore for first-time stillOS users.");
            if (driver_reboot_required) {
                description += tr("\n\nDrivers will apply on reboot.");
            }
            return description;
        }

        private void open_still_explore() {
            var subprocess = new SubprocessLauncher(SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE);
            try {
                subprocess.spawnv({"/usr/bin/stillexplore"});
            } catch (Error e) {
                // Try alternative launch methods
                try {
                    subprocess.spawnv({"/usr/bin/gtk-launch", "stillexplore"});
                } catch (Error e2) {
                    warning("Failed to open stillExplore: %s", e2.message);
                }
            }
        }

        private void complete_setup() {
            context.window.ensure_browser_pinned();

            // Disable the startup-show GSettings key so Quick Setup doesn't run again
            try {
                var settings = new GLib.Settings("io.stillhq.quicksetup");
                settings.set_boolean("startup-show", false);
                print("GSettings startup-show set to false\n");
            } catch (Error e) {
                warning("Failed to update GSettings: %s", e.message);
            }
        }
    }
}
