"""
Setup script for screenshot-capture skill.
Installs required Python packages and checks for Tesseract OCR.
"""
import subprocess
import sys
import shutil

REQUIRED_PACKAGES = [
    "Pillow",
    "mss",
    "pywin32",
    "pytesseract",
    "pygetwindow",
]


def install_packages():
    """Install required pip packages."""
    for pkg in REQUIRED_PACKAGES:
        print(f"Checking {pkg}...")
        try:
            subprocess.check_call(
                [sys.executable, "-m", "pip", "install", pkg, "--quiet"],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
            )
            print(f"  {pkg} — OK")
        except subprocess.CalledProcessError:
            print(f"  {pkg} — FAILED to install. Try: pip install {pkg}")


def check_tesseract():
    """Check if Tesseract OCR binary is available on PATH."""
    tesseract_path = shutil.which("tesseract")
    if tesseract_path:
        print(f"\nTesseract OCR found at: {tesseract_path}")
        try:
            version = subprocess.check_output(
                ["tesseract", "--version"], stderr=subprocess.STDOUT
            ).decode().split("\n")[0]
            print(f"  Version: {version}")
        except Exception:
            pass
        return True
    else:
        print("\n*** Tesseract OCR is NOT installed. ***")
        print("OCR features will not work without it.")
        print("Install from: https://github.com/UB-Mannheim/tesseract/wiki")
        print("Make sure the install directory is on your system PATH.")
        print("(Capture and annotation will still work without Tesseract.)")
        return False


def main():
    print("=== Screenshot Capture Skill — Setup ===\n")
    print("Installing Python packages...\n")
    install_packages()
    print("\nChecking Tesseract OCR...\n")
    check_tesseract()
    print("\nSetup complete.")


if __name__ == "__main__":
    main()
