# setup.py
"""
Setup script for ankur-scraper with Playwright auto-installation.
"""

import subprocess
import sys
from setuptools import setup, find_packages
from setuptools.command.install import install


class PostInstallCommand(install):
    """Post-installation hook to install Playwright browsers."""
    
    def run(self):
        # Run the standard install
        install.run(self)
        
        # Try to install Playwright browsers
        print("\n" + "=" * 60)
        print("Installing Playwright browsers...")
        print("=" * 60)
        
        try:
            # Install chromium browser
            subprocess.check_call([
                sys.executable, "-m", "playwright", "install", "chromium"
            ])
            print("\n✅ Playwright Chromium browser installed successfully!")
            print("=" * 60 + "\n")
        except subprocess.CalledProcessError as e:
            print("\n⚠️  Playwright browser installation failed.")
            print("You can install it manually later with:")
            print("    playwright install chromium")
            print("=" * 60 + "\n")
        except Exception as e:
            print(f"\n⚠️  Unexpected error during Playwright installation: {e}")
            print("You can install it manually later with:")
            print("    playwright install chromium")
            print("=" * 60 + "\n")


# Read dependencies from pyproject.toml or define here
INSTALL_REQUIRES = [
    "requests>=2.31.0",
    "beautifulsoup4>=4.12.0",
    "lxml>=4.9.0",
    "playwright>=1.40.0",
    "tldextract>=5.0.0",
    "rich>=13.0.0",
    "httpx>=0.24.0",
]


setup(
    name="ankur-scraper",
    version="0.1.6",
    description="A robust website scraper with intelligent content extraction",
    long_description=open("README.md", encoding="utf-8").read() if __file__ else "",
    long_description_content_type="text/markdown",
    author="Ankur Dev",
    author_email="Dev@ankursolutions.com",
    url="https://github.com/AnkurSolutions/ankur-scraper",
    packages=find_packages(),
    python_requires=">=3.8",
    install_requires=INSTALL_REQUIRES,
    entry_points={
        "console_scripts": [
            "ankur-scraper=ankur_scraper.cli:main",
            "ankur_scraper=ankur_scraper.cli:main",
        ],
    },
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Topic :: Internet :: WWW/HTTP",
    ],
    cmdclass={
        "install": PostInstallCommand,
    },
    zip_safe=False,
)