"""Adw.Application-Einstiegspunkte fürs GTK-Frontend.

Zwei Apps auf derselben Codebasis (teilen core/ + die GUI-Bausteine):

    tsettings   System Settings  — Kernel/Connectivity/Hardware/…
    thub        Software Hub      — Pakete suchen/installieren, Updates…

Jede ist nur ein dünner Shell, der seine Sektionsliste an MainWindow gibt.
"""
from __future__ import annotations

import sys

import gi

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Adw, Gio  # noqa: E402

from taninux.gui import APP_ID, style
from taninux.gui.pages import HUB_SECTIONS, SETTINGS_SECTIONS
from taninux.gui.window import MainWindow


class _ShellApp(Adw.Application):
    def __init__(self, app_id: str, title: str, sections) -> None:
        super().__init__(application_id=app_id, flags=Gio.ApplicationFlags.DEFAULT_FLAGS)
        self._title = title
        self._sections = sections
        self._window: MainWindow | None = None

    def do_activate(self) -> None:
        if self._window is None:
            style.apply_persisted()  # gespeichertes Farbschema + Fuji-Akzent
            self._window = MainWindow(self, self._sections, self._title)
        self._window.present()


def main_settings(argv: list[str] | None = None) -> int:
    app = _ShellApp(f"{APP_ID}.Settings", "System Settings", SETTINGS_SECTIONS)
    return app.run(argv if argv is not None else sys.argv)


def main_hub(argv: list[str] | None = None) -> int:
    app = _ShellApp(f"{APP_ID}.SoftwareHub", "Software Hub", HUB_SECTIONS)
    return app.run(argv if argv is not None else sys.argv)


# Rückwärtskompatibel: `taninux-gtk` / `python -m taninux.gui` -> System Settings.
main = main_settings


if __name__ == "__main__":
    raise SystemExit(main())
