from __future__ import annotations
import urllib.request
from PyQt6.QtCore import QThread, pyqtSignal


class OllamaCheckWorker(QThread):
    reachable = pyqtSignal(bool, str)  # (ok, url_tried)

    def __init__(self, host_url: str, timeout: int = 4, parent=None):
        super().__init__(parent)
        self._url = host_url.rstrip("/") + "/api/tags"
        self._timeout = timeout

    def run(self):
        try:
            urllib.request.urlopen(self._url, timeout=self._timeout)
            self.reachable.emit(True, self._url)
        except Exception:
            self.reachable.emit(False, self._url)
