#!/usr/bin/env python3
import sys, os
from main import finderConsole
from abstract_gui.QT6.utils.console_utils.start_console import startConsole
from abstract_gui.QT6.utils.shared_bus import SharedStateBus
from abstract_paths import reset_find_console_stop

def main():
    # ── Determine target directory ────────────────────────────────
    target_dir = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
    if not os.path.isdir(target_dir):
        target_dir = os.path.dirname(target_dir)
    os.chdir(target_dir)  # real working directory for the console

    os.environ.setdefault("QT_QPA_PLATFORM", "xcb")
    os.environ.setdefault("QT_STYLE_OVERRIDE", "Fusion")

    # ── Init bus + console ────────────────────────────────────────
    reset_find_console_stop()
    bus = SharedStateBus()

    def console_factory():
        win = finderConsole(bus=bus)
        win.setWindowTitle(f"Abstract Finder Console — {target_dir}")
        # try to set dir_in on all tabs
        for i in range(win.layout().count()):
            w = win.layout().itemAt(i).widget()
            if hasattr(w, "dir_in"):
                w.dir_in.setText(target_dir)
        return win

    startConsole(console_factory)

if __name__ == "__main__":
    main()
