import requests
from requests.adapters import HTTPAdapter, Retry
from xml.etree import ElementTree as ET
import time
import os

api_url = "https://www.vali.bg/api/v1/products"
api_token = "RBOSPOcFseFV9SKnqNnk2vHJLxencXe3mXl8ukPyhnUyZT54rHoTsabrWiMD"
per_page = 1000

# Create a session with retries
session = requests.Session()
retry_strategy = Retry(
    total=5,
    backoff_factor=2,
    status_forcelist=[429, 500, 502, 503, 504],
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount("https://", adapter)
session.mount("http://", adapter)

# Add custom User-Agent
session.headers.update({
    "User-Agent": "ValiProductsFetcher/1.0 (+https://towershop.gr)",
    "Accept": "application/xml"
})

def fetch_products(page, per_page, api_token):
    params = {
        "page": page,
        "per_page": per_page,
        "api_token": api_token
    }

    try:
        response = session.get(api_url, params=params, timeout=20)
        response.raise_for_status()
        return response.text
    except requests.exceptions.RequestException as e:
        raise Exception(f"Failed to fetch page {page}: {e}")

def combine_products():
    all_products = ET.Element("products")

    # First request to get the total number of items
    data = fetch_products(1, 10, api_token)
    root = ET.fromstring(data)
    total_items = int(root.find("total_items").text)
    pages = total_items // per_page + (1 if total_items % per_page > 0 
else 0)

    print(f"Total items: {total_items}, Pages: {pages}")

    # Fetch all pages with 1000 products per page
    for page in range(1, pages + 1):
        print(f"Fetching page {page}/{pages}...")
        data = fetch_products(page, per_page, api_token)
        root = ET.fromstring(data)

        products = root.find("products")
        for product in products:
            all_products.append(product)

    return ET.tostring(all_products, encoding='utf-8').decode('utf-8')


if __name__ == "__main__":
    combined_xml = combine_products()
    script_dir = os.path.dirname(os.path.realpath(__file__))
    output_path = os.path.join(script_dir, "combined_products.xml")

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(combined_xml)

    print(f"Combined XML saved to {output_path}")

