import requests
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

def fetch_products(page, per_page, api_token):
    params = {
        "page": page,
        "per_page": per_page,
        "api_token": api_token
    }
    
    max_retries = 5
    retries = 0
    while retries < max_retries:
        try:
            response = requests.get(api_url, params=params)
            response.raise_for_status()
            return response.text
        except (requests.exceptions.RequestException, requests.exceptions.ConnectionError) as e:
            retries += 1
            print(f"Retry {retries}/{max_retries} for page {page} due to error: {e}")
            time.sleep(2 ** retries)  # Exponential backoff
    
    raise Exception(f"Failed to fetch page {page} after {max_retries} retries")

def combine_products():
    all_products = ET.Element("products")

    # First request to get the total number of items and pages
    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)

    # Fetch all pages with 1000 products per page
    for page in range(1, pages + 1):
        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}")
