"""Scrape StewMac products via sitemap parsing."""

import argparse
import csv
import re
from datetime import date
from pathlib import Path
from typing import Dict, List

import requests


def _fetch_xml(url: str) -> str:
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
    }
    try:
        resp = requests.get(url, headers=headers, timeout=30)
        if resp.status_code != 200:
            return ""
        return resp.text
    except Exception:
        return ""


def _parse_sitemap_urls(xml_content: str) -> List[str]:
    return re.findall(r"<loc>(.*?)</loc>", xml_content)


def _scrape_stewmac_sitemap(limit: int = 50000) -> List[Dict[str, str]]:
    results = []
    base_sitemap = "https://www.stewmac.com/sitemap.xml"

    xml_content = _fetch_xml(base_sitemap)
    if not xml_content:
        return []

    all_urls = _parse_sitemap_urls(xml_content)

    keep_categories = [
        "/luthier-tools-and-supplies/", "/parts-and-hardware/",
        "/tonewoods/", "/electronics/",
        "/kits-and-projects/", "/strings-and-accessories/",
    ]
    exclude_categories = [
        "/video-and-ideas/", "/customer-service/", "/account/",
        "/cart/", "/search/", "/login/", "/checkout/",
    ]

    count = 0
    for url in all_urls:
        if count >= limit:
            break
        if any(ex in url for ex in exclude_categories):
            continue
        if not any(keep in url for keep in keep_categories):
            continue

        clean_url = url.split("?")[0].rstrip("/")
        parts = clean_url.split("/")
        if len(parts) < 4:
            continue

        slug = parts[-1]
        category_slug = parts[-2]
        name = slug.replace("-", " ").title()
        category = category_slug.replace("-", " ").title()

        row = {
            "id": f"sm_{slug}",
            "name": name,
            "seller": "StewMac",
            "species": "See Link",
            "price": "Check Site",
            "listed_date": date.today().isoformat(),
            "status": "Active",
            "image_url": "",
            "link": url,
            "dimensions": "",
            "figure": "",
            "category": category,
            "raw_attributes": "Sitemap Scrape",
        }
        results.append(row)
        count += 1

    return results


def _write_csv(path: str, rows: List[Dict[str, str]]) -> None:
    if not rows:
        return
    fieldnames = list(rows[0].keys())
    Path(path).parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)


def run(data_dir: str, limit: int = 50000) -> str:
    """Run the StewMac scraper, writing output to data_dir/raw/stewmac_latest.csv."""
    output_path = str(Path(data_dir) / "raw" / "stewmac_latest.csv")
    rows = _scrape_stewmac_sitemap(limit)
    if not rows:
        return "No products returned from StewMac."
    _write_csv(output_path, rows)
    return f"Scraped {len(rows)} items from StewMac to {output_path}"


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--limit", type=int, default=50000)
    parser.add_argument("--output", default="data/raw/stewmac_latest.csv")
    args = parser.parse_args()
    data = _scrape_stewmac_sitemap(args.limit)
    _write_csv(args.output, data)
    print(f"Scraped {len(data)} items from StewMac.")
