import requests
import csv
import xml.etree.ElementTree as ET
import os

API_KEY = 'c98e36cd9219672eb9cfe825ba5161f40e4a107f965e17d719'
CATEGORY_ID = '46'
LIMIT_PRODUCTS = 1000  # Ограничение для теста
LOG_FILE = '../printplius-sync/error_log.txt'
CSV_FILE = '../printplius-sync/products_batch-15.csv'

TRANSLATION_MAP = {
    "1 - 2 working days": "1-2 tööpäeva",
    "2 - 3 working days": "2-3 tööpäeva",
    "4 - 6 working days": "4-6 tööpäeva"
}

def translate_variation_name(name):
    return TRANSLATION_MAP.get(name, name)

def write_to_log(message):
    with open(LOG_FILE, 'a') as f:
        f.write(message + '\n')

def get_products(api_key, category_id, start=0, limit=100):
    url = f'https://b2b.printplius.lt/apiclient/xml/getProducts?apiKey={api_key}&id_category={category_id}&start={start}&limit={limit}'
    response = requests.get(url)
    if response.status_code == 200:
        return ET.fromstring(response.content)
    else:
        raise Exception(f"Failed to get products: {response.content.decode()}")

def get_product_features(api_key, product_id):
    url = f'https://b2b.printplius.lt/apiclient/xml/getProductFeatures?apiKey={api_key}&id_product={product_id}'
    response = requests.get(url)
    if response.status_code == 200:
        return ET.fromstring(response.content)
    else:
        raise Exception(f"Failed to get product features: {response.content.decode()}")

def create_csv_file(products, filename='products_test_batch.csv'):
    # Динамически формируем заголовок
    header = [
        "id_product", "name", "description", "manufacturer_name", "sku",
        "variation_name", "variation_name_et", "price_with_pvm", "price_with_margin", "quantity"
    ]

    # Найдем максимальное количество изображений и атрибутов
    max_images = 0
    max_attributes = 0
    for product in products:
        if len(product["images"]) > max_images:
            max_images = len(product["images"])
        if len(product["features"]) > max_attributes:
            max_attributes = len(product["features"])

    # Добавим заголовки для всех изображений
    for i in range(1, max_images + 1):
        header.append(f"image{i}")

    # Добавим заголовки для всех атрибутов
    for i in range(1, max_attributes + 1):
        header.append(f"attribute{i}_name")
        header.append(f"attribute{i}_value")

    with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=header)
        writer.writeheader()

        for product in products:
            for i, price_info in enumerate(product["prices"], start=1):
                if i >= 4:  # Skip if there are more than 3 prices
                    break

                price_with_pvm = round(price_info["price_with_pvm"], 2)
                price_with_margin = round(price_with_pvm * 1.15, 2)
                variation_name = price_info["name"]
                variation_name_et = translate_variation_name(price_info["name"])

                product_row = {
                    "id_product": product["id_product"],
                    "name": product["name"],
                    "description": product["description"],
                    "manufacturer_name": product["manufacturer_name"],
                    "sku": product["sku"],  # Используем поле reference для SKU
                    "variation_name": variation_name,
                    "variation_name_et": variation_name_et,
                    "price_with_pvm": price_with_pvm,
                    "price_with_margin": price_with_margin,
                    "quantity": int(product["quantities"][i-1]["quantity"]) if i-1 < len(product["quantities"]) else 0,
                }

                # Добавим все изображения
                for j in range(1, max_images + 1):
                    product_row[f"image{j}"] = product["images"][j-1] if j-1 < len(product["images"]) else ""

                # Добавим все атрибуты
                for j, feature in enumerate(product["features"], start=1):
                    if j > max_attributes:
                        break
                    product_row[f"attribute{j}_name"] = feature["name"]
                    product_row[f"attribute{j}_value"] = feature["value"]

                writer.writerow(product_row)

    print(f"CSV file '{filename}' created successfully.")

def main():
    start = 0
    products_list = []

    print("Starting product data collection...")

    while len(products_list) < LIMIT_PRODUCTS:
        try:
            products_xml = get_products(API_KEY, CATEGORY_ID, start=start, limit=100)
            products = products_xml.findall('./products/product')
            if not products:
                break

            for product in products:
                try:
                    product_data = {
                        "id_product": product.find('id_product').text,
                        "reference": product.find('reference').text,
                        "on_sale": product.find('on_sale').text,
                        "online_only": product.find('online_only').text,
                        "ean13": product.find('ean13').text if product.find('ean13') is not None else '',
                        "ecotax": product.find('ecotax').text if product.find('ecotax') is not None else '',
                        "minimal_quantity": product.find('minimal_quantity').text if product.find('minimal_quantity') is not None else '',
                        "additional_shipping_cost": product.find('additional_shipping_cost').text if product.find('additional_shipping_cost') is not None else '',
                        "quantity_discount": product.find('quantity_discount').text if product.find('quantity_discount') is not None else '',
                        "description": product.find('description').text if product.find('description') is not None else '',
                        "description_short": product.find('description_short').text if product.find('description_short') is not None else '',
                        "name": product.find('name').text,
                        "manufacturer_name": product.find('manufacturer_name').text if product.find('manufacturer_name') is not None else '',
                        "rate": product.find('rate').text if product.find('rate') is not None else '',
                        "sku": product.find('reference').text if product.find('reference') is not None else '',  # Используем поле reference для SKU
                        "prices": [],
                        "quantities": [],
                        "images": [],
                        "features": []
                    }

                    prices = product.find('prices')
                    if prices is not None and len(prices) > 0:
                        for price in prices.findall('price'):
                            price_data = {
                                "id_product_attribute": price.find('id_product_attribute').text,
                                "name": price.find('name').text,
                                "price_with_pvm": float(price.find('price_with_pvm').text)
                            }
                            product_data["prices"].append(price_data)

                    quantities = product.find('quantities')
                    if quantities is not None and len(quantities) > 0:
                        for quantity in quantities.findall('quantity'):
                            quantity_data = {
                                "id_product_attribute": quantity.find('id_product_attribute').text,
                                "name": quantity.find('name').text,
                                "quantity": quantity.find('quantity').text
                            }
                            product_data["quantities"].append(quantity_data)

                    images = product.find('images')
                    if images is not None and len(images) > 0:
                        for image in images.findall('image'):
                            image_data = image.text
                            product_data["images"].append(image_data)

                    features_xml = get_product_features(API_KEY, product_data["id_product"])
                    features_data = []
                    for feature in features_xml.findall('./features/feature'):
                        feature_data = {
                            "name": feature.find('name').text,
                            "value": feature.find('value').text,
                            "custom": feature.find('custom').text
                        }
                        features_data.append(feature_data)

                    product_data["features"] = features_data
                    products_list.append(product_data)

                    if len(products_list) >= LIMIT_PRODUCTS:
                        break

                    print(f"Processed product ID {product_data['id_product']} - {product_data['name']}")

                except Exception as e:
                    error_message = f"Error processing product ID {product.find('id_product').text if product.find('id_product') is not None else 'unknown'}: {str(e)}"
                    write_to_log(error_message)
                    print(error_message)

            if len(products_list) >= LIMIT_PRODUCTS:
                break

            start += 100

        except Exception as e:
            error_message = f"Error fetching product batch starting at {start}: {str(e)}"
            write_to_log(error_message)
            print(error_message)

    print("Finished product data collection.")

    create_csv_file(products_list, CSV_FILE)

if __name__ == "__main__":
    main()
    print("Script finished.")