import requests
import json
import time

SUBCATS = {
    "makeup": ["makeup"],
    "skincare": ["skin-care"],
    "haircare": ["hair-care"],
    "fragrance": ["perfumes"],
    "bath_body": ["shower-gels", "soaps"],
    "mens_grooming": ["men-s-cosmetics"],
    "beauty_tools": [],
    "wellness_nutrition": []
}

NAME_MAP = {
    "makeup": "Makeup",
    "skincare": "Skincare",
    "haircare": "Haircare",
    "fragrance": "Fragrance",
    "bath_body": "Bath & Body",
    "mens_grooming": "Men's Grooming",
    "beauty_tools": "Beauty Tools",
    "wellness_nutrition": "Wellness & Nutrition"
}

HEADERS = {"User-Agent": "BuyzzApp/1.0 (test@example.com)"}

products = []
seen_ids = set()

for sub_id, categories in SUBCATS.items():
    for cat in categories:
        url = "https://world.openbeautyfacts.org/api/v2/search"
        params = {"categories_tags_en": cat, "page_size": 40, "fields": "code,product_name,image_front_url,image_url,brands,generic_name"}
        data = None
        for attempt in range(4):
            try:
                r = requests.get(url, params=params, headers=HEADERS, timeout=15)
                data = r.json()
                break
            except Exception as e:
                time.sleep(3)
        if data is None:
            print("skip", cat, "failed after retries")
            continue
        for p in data.get("products", []):
            pid = p.get("code")
            name = p.get("product_name")
            image = p.get("image_front_url") or p.get("image_url")
            if not pid or not name or not image or pid in seen_ids:
                continue
            seen_ids.add(pid)
            products.append({
                "id": pid,
                "name": name,
                "image": image,
                "source": "obf",
                "brand": p.get("brands", ""),
                "description": p.get("generic_name", ""),
                "sub": NAME_MAP[sub_id],
                "attributes": {}
            })
        print(cat, "done, total so far:", len(products))

with open("beauty.json", "w") as f:
    json.dump({"products": products}, f)

print("TOTAL:", len(products))
