import yaml
from scraper.html import fetch_html
from scraper.js import fetch_js
from scraper.parser import parse_html

class Fetcher:
    def __init__(self, sites_config="config/sites.yml"):
        self.sites = self.load_yaml(sites_config)

    def load_yaml(self, file_path):
        """Loads YAML and normalizes site names (lowercase, underscores)."""
        with open(file_path, "r") as file:
            data = yaml.safe_load(file)

        # Convert names: "Airdrops.io" → "airdrops_io"
        self.sites_dict = {
            site["name"].lower().replace(" ", "_").replace(".", ""): site["url"]
            for category in data.values() for site in category
        }

        print(f"Loaded sites: {self.sites_dict}")  # Debugging line
        return self.sites_dict

    def fetch(self, site_name, debug=False):
        """Fetches data from a given site using a normalized name."""
        site_name = site_name.lower().replace(" ", "_").replace(".", "")  # Normalize input
        url = self.sites.get(site_name)

        if not url:
            raise ValueError(f"Site '{site_name}' not found in sites.yml")

        return fetch_js(url, debug) if "coinmarketcap" in url else fetch_html(url, debug)
    
    def fetch_and_parse(self, site_name):
        """Fetch data from a live site and parse it immediately (production mode)."""
        print(f"🚀 Fetching live data from: {site_name}")

        # Normalize site name (convert to lowercase & replace spaces)
        normalized_name = site_name.lower().replace(" ", "_").replace(".", "")

        url = self.sites.get(normalized_name)

        if not url:
            raise ValueError(f"❌ Site '{site_name}' not found in sites.yml")

        print(f"🔍 DEBUG: Fetching URL: {url}")

        html_content = fetch_js(url) if "coinmarketcap" in url else fetch_html(url)
        parsed_data = parse_html(html_content)

        print(f"✅ Extracted Data: {parsed_data}")
        return parsed_data




