from pathlib import Path
import subprocess
import sys
import os


class Application(object):
    def __init__(self, *args, **kwargs):
        for key in kwargs:
            setattr(self, key, kwargs[key])
        self.base_path = Path(__file__).resolve().parents[1]
        self.venv_path = self.base_path / "venv"
        self.python = self.venv_path / "bin" / "python"
        self._hello()

    def _hello(self):
        print(f"Iniciando o {getattr(self, 'package', '')} {getattr(self, 'version', '')}...")

    def _print(self, msg: str):
        print(f"[{getattr(self, 'package', '')}] {msg}", flush=True)

    def run(self):
        self._ensure_venv()
        self.launch_neuro()

    def _ensure_venv(self):
        os.chdir(self.base_path)

        if not self.venv_path.exists():
            self._print("Criando ambiente Python...")
            subprocess.run(
                [sys.executable, "-m", "venv", str(self.venv_path)],
                check=True
            )
        else:
            self._print("Ambiente Python já existente.")

    def launch_neuro(self):
        neuro = self.base_path / "src" / "neuro" / "__main__.py"
        subprocess.run([str(self.python), str(neuro)])



