import requests
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET

# Initialize a session
session = requests.Session()

# Define login credentials and URLs
login_url = 'https://www.service-parts.gr/index.php?route=account/login'
category_url = 'https://www.service-parts.gr/bataries-laptop'
username = 'g.hatzidelios@towershop.gr'
password = 'seawolf'

# Login payload
login_payload = {
    'email': username,
    'password': password,
}

# Log in to the website
session.post(login_url, data=login_payload)

# Function to extract product details
def extract_product_details(product_url):
    print(f"Fetching product details from: {product_url}")
    product_page = session.get(product_url)
    product_soup = BeautifulSoup(product_page.content, 'html.parser')
    
    # Extract the title
    title_tag = product_soup.find('h1')
    if title_tag:
        title = title_tag.text.strip()
        print(f"Title: {title}")
    else:
        print(f"Title not found for URL: {product_url}")
        return None  # Skip this product if the title is not found
    
    # Extract the price
    price_tag = product_soup.find('h2')
    if price_tag:
        price = price_tag.text.strip()
        print(f"Price: {price}")
    else:
        print(f"Price not found for URL: {product_url}")
        return None  # Skip this product if the price is not found
    
    # Extract the stock status from product page
    availability_tag = product_soup.find('li', string=lambda t: t and 'Διαθεσιμότητα:' in t)
    if availability_tag:
        availability = availability_tag.text.split(':')[-1].strip()
        print(f"Availability: {availability}")
        if availability.lower() == "εξαντλημένο":
            print(f"Product is out of stock at URL: {product_url}")
            return None  # Skip this product if it's out of stock
    else:
        print(f"Availability not found for URL: {product_url}")
        return None
    
    # Extract the product code
    product_code_tag = product_soup.find('li', string=lambda t: t and 'Κωδικός προϊόντος:' in t)
    if product_code_tag:
        product_code = product_code_tag.text.split(':')[-1].strip()
        print(f"Product Code: {product_code}")
    else:
        print(f"Product code not found for URL: {product_url}")
        return None
    
    # Extract the correct image URL (larger size image)
    thumbnail_tag = product_soup.find('a', class_='thumbnail')
    if thumbnail_tag:
        img_url = thumbnail_tag['href']  # Get the href attribute for the larger image
        print(f"Image URL: {img_url}")
    else:
        print(f"Image not found for URL: {product_url}")
        return None
    
    # Extract the description content only (without the surrounding <div> tag)
    description_tag = product_soup.find('div', id='tab-description')
    if description_tag:
        description = ''.join(str(content) for content in description_tag.contents).strip()
        print("Description fetched.")
    else:
        print(f"Description not found for URL: {product_url}")
        return None
    
    return {
        'title': title,
        'price': price,
        'product_code': product_code,  # Now correctly defined
        'availability': availability,
        'description': description,
        'img_url': img_url
    }


# Function to create a CDATA section manually
def create_cdata_element(parent, tag, text):
    element = ET.SubElement(parent, tag)
    element.text = text  # This will escape special characters like <, >, and &
    return element

# Create XML structure
root = ET.Element('Products')  # The root element is now <Products>

# Loop through all pages in the category
current_page = 1
while True:
    print(f"Fetching page {current_page}...")
    page_url = f"{category_url}&page={current_page}"
    page = session.get(page_url)
    soup = BeautifulSoup(page.content, 'html.parser')
    
    products = soup.find_all('div', class_='product-layout product-list col-xs-12')
    
    if not products:
        print("No more products found. Exiting loop.")
        break  # No more products found
    
    for product in products:
        # Check stock status from the category page
        stock_status = product.find('span', class_='label label-info labelstat')
        if stock_status and stock_status.text == "Σε Απόθεμα":
            product_link = product.find('a')['href']
            product_details = extract_product_details(product_link)
            if not product_details:
                continue
            
            product_element = ET.SubElement(root, 'Product')  # Each product is within a <Product> tag
            
            title_element = ET.SubElement(product_element, 'Title')
            title_element.text = product_details['title']
            
            price_element = ET.SubElement(product_element, 'Price')
            price_element.text = product_details['price']
            
            product_code_element = ET.SubElement(product_element, 'ProductCode')
            product_code_element.text = product_details['product_code']
            
            availability_element = ET.SubElement(product_element, 'Availability')
            availability_element.text = product_details['availability']
            
            create_cdata_element(product_element, 'Description', product_details['description'])
            
            img_element = ET.SubElement(product_element, 'ImageURL')
            img_element.text = product_details['img_url']
        else:
            print("Product is out of stock on the category page; skipping.")
    
    print(f"Finished processing page {current_page}.")
    current_page += 1

# Write to XML file in the same directory as the script
output_file = 'products.xml'
print(f"Writing results to {output_file}...")
tree = ET.ElementTree(root)
tree.write(output_file, encoding='utf-8', xml_declaration=True)
print("Done!")