from woocommerce import API
import pandas as pd
import ezodf
import requests
import pprint
import base64
import time
import json

wcapi = API(
    url="https://lescreationsdemissbleue.local",
    consumer_key="ck_604e9b7b5d290cce72346efade6b31cb9a1ff28e",
    consumer_secret="cs_563974c7e59532c1ae1d0f8bbf61f0500d6bc768",
    wp_api=True,
    version="wc/v3",
    verify_ssl=False # Désactive la vérification SSL pour le développement
)

class Authentification:  
    # Identifiants WordPress (et non WooCommerce)
    wordpress_username = "admin_lcdm"  # Remplace par ton username WordPress
    wordpress_application_password = "yTW8 Mc6J FUCN tPSq bnuJ 0Sdw" #"#8io_mb!55@Bis"  # Généré dans WordPress > Utilisateurs

    # Générer l'authentification Basic en base64
    auth_str = f"{wordpress_username}:{wordpress_application_password}"
    auth_bytes = auth_str.encode("utf-8")
    auth_base64 = base64.b64encode(auth_bytes).decode("utf-8")
    
ath = Authentification()

path_name = "C:\\Users\\beren\\OneDrive\\Documents\\nextcloud\\beren\\site_missbleue\\photos\\Photos_Claudine\\Photos_bougies_Claudine"

images_to_upload = [
    {'categories': [
        (f"{path_name}\\Bougies\\Chope\\chope-sapin-face.jpg", "chope-sapin-face.jpg", "Chope sapin", "Chope sapin de face", "Gamme prestige"),
        (f"{path_name}\\Bougies\\Chope\\chope-adoucissant-face.jpg", "chope-adoucissant-face.jpg", "Chope adoucissant", "Chope adoucissant de face", "Chopes"),
        ]
    },
    {'products':[
        (f"{path_name}\\Bougies\\Chope\\chope-citron-meringue-face.jpg", "chope-citron-meringue-face-1.jpg", "Chope citron meringuée (1)", "Chope citron meringuée de face", "Chopes"),
        (f"{path_name}\\Bougies\\Chope\\chope-citron-meringue-haut.jpg", "chope-citron-meringue-haut-1.jpg", "Chope citron meringuée (2)", "Chope citron meringuée de haut", "Chopes"),
        (f"{path_name}\\Bougies\\Chope\\chope-citron-meringue-dos.jpg", "chope-citron-meringue-dos-1.jpg", "Chope citron meringuée (3)", "Chope citron meringuée de dos", "Chopes")
        ]
    }
]

category_parent_id = 0
categories_to_create  = [
    ("Gamme prestige", "Catégorie principale pour organiser la gamme prestige", None),
    ("Chopes", "Catégorie enfant de la gamme prestige", category_parent_id),
]

#media_api_url = f"https://lescreationsdemissbleue.local/wp-json/wp/v2/media"

class ImageManager:
    
    def __init__(self, images_to_upload, ath, url_website):
        self.images_to_upload = images_to_upload
        self.ath = ath
        self.media_api_url = f"{url_website}/wp-json/wp/v2/media"
        self.dict_image = {}
        self.headers = {
            "Authorization": f"Basic {self.ath.auth_base64}",
            "Content-Type": "application/json"
        }

    def update_data(self, title, alt, image_id):
        updata_data = {
                "title" : title,
                "alt_text": alt
            }
             
        response = requests.post(
            f"{self.media_api_url}/{image_id}",
            headers=self.headers,
            json=updata_data,
            verify=False 
        )
 
        if response.status_code == 200:
            return response.json() 
        else:
            return None

    

    def upload_image(self, image_path, image_name, title, alt, category_name):
        with open(image_path, "rb") as image_file:
            response = requests.post(
                self.media_api_url,
                headers={
                    "Authorization": f"Basic {self.ath.auth_base64}",
                    "Content-Disposition": f"attachment; filename={image_name}"
                },
                files={"file": image_file},
                verify=False
            )
            
        if response.status_code == 201:
            media_data = response.json()
            self.update_data(title, alt, media_data["id"])   
            return {'category_name':category_name, 'id':media_data["id"], 'image_name':image_name, 'title':title}  # Retourne l'ID de l'image
        else:
            return None
        
    def update(self):
        uploaded_images_cat = []    
        for image_path, image_name, title, alt, category_name in images_to_upload[0]['categories']:
            category_dict = self.upload_image(image_path, image_name, title, alt, category_name)
            uploaded_images_cat.append(category_dict)
                
        uploaded_images_pro = []
        for image_path, image_name, title, alt, category_name in images_to_upload[1]['products']:
            image_dict = self.upload_image(image_path, image_name, title, alt, category_name)
            uploaded_images_pro.append(image_dict)
        
        self.dict_image['category'] = uploaded_images_cat
        self.dict_image['product'] = uploaded_images_pro
        
        return self.dict_image
    
    def delete(self):
        pass
        
class CategoryManager:
    
    def __init__(self, wcapi, ath, categories_to_create, url_website, uploaded_images):
        self.wcapi = wcapi
        self.ath = ath
        self.categories_to_create = categories_to_create
        self.categories_api_url = f"{url_website}/wp-json/wp/v2/product_cat" 
        self.uploaded_images_cat = uploaded_images
        
        self.media_api_url = f"{url_website}/wp-json/wp/v2/media"
        self.headers = {
            "Authorization": f"Basic {self.ath.auth_base64}",
            "Content-Type": "application/json"
        }

    def create_category(self, name, description, parent_id=None):
        category_data = {
            "name": name,
            "description": description,
            "parent": parent_id if parent_id else 0, 
        }   
        response = self.wcapi.post("products/categories/", category_data)
        if response.status_code == 201:
            category = response.json()
            return category
        else:
            return None
      
    def get_id_img_for_category(self, category):
        for cat_img in self.uploaded_images_cat:
            if category['name'] == cat_img['category_name']:            
                image_id = {'id':cat_img['id']}
                #break
        return image_id        
        
    def update_data_img_id_for_category(self, img_id, cat_id):
        update_category_data = {
                "image" : img_id,
            } 
        self.wcapi.put(f"products/categories/{cat_id}", update_category_data)

    def update(self):
        for category in self.categories_to_create:
            name, description, parent_id = category
            if parent_id != None:
                parent_id = cat['id']
            cat = self.create_category(name, description, parent_id)
            img_id = self.get_id_img_for_category(cat)
            self.update_data_img_id_for_category(img_id,cat["id"])
    
    def delete_category(self):
        response = self.wcapi.get(f"products/categories")
        for cat in response.json():
            print(f"cat_image = {cat['image']}")
            for category in self.categories_to_create:
                name, description, parent_id = category
                if cat['name'] == name:
                    response = self.wcapi.delete(f"products/categories/{cat['id']}", params={"force": True})
    
    def delete_img_category(self):
        for img_cat in self.uploaded_images_cat:
            print(f"img_cat['id'] = {img_cat['id']}")
            response = requests.delete(
                f"{self.media_api_url}/{img_cat['id']}",
                headers=self.headers,
                verify=False 
        )
            
    def delete_all_img_cat_by_name(self):
        response = requests.get(
            f"{self.media_api_url}?per_page=100",
            headers=self.headers,
            verify=False 
        )
        images = response.json()
        for img in images:    
            for path_image, name_image, title_image, text_alt, category_name in self.uploaded_images_cat:
                if img['title']['rendered'] == title_image:
                    print(f"img_id = {img['id']}")
                    print('iiii')
                    response = requests.delete(
                        f"{self.media_api_url}/{img['id']}?force=true",
                        headers=self.headers,
                        verify=False
                    )

                    print(f"Status Code: {response.status_code}")
                    pprint.pprint(response.json())
            
    def delete(self):
        self.delete_category()
        #self.delete_img_category()
        self.delete_all_img_cat_by_name()
            
class ProductManager:
    def __init__(self, wcapi, ath, filename_ods, uploaded_images):
        self.wcapi = wcapi
        self.ath = ath
        self.uploaded_images_pro = uploaded_images
        self.filename_ods = filename_ods
        
        self.media_api_url = f"{url_website}/wp-json/wp/v2/media"
        self.headers = {
            "Authorization": f"Basic {self.ath.auth_base64}",
            "Content-Type": "application/json"
        }

    def get_list_id_img_for_product(self, product):
        image_id = {}
        list_image_id_for_product = []
        list_images_by_doc = [img.strip().replace('"', '') for img in product['Photo'].split(",")]
        #pprint.pprint(self.uploaded_images_pro)
        for pro_img in self.uploaded_images_pro:
            for image in list_images_by_doc:
                #print(f"list_images_by_doc = {list_images_by_doc}")
                if image == pro_img['image_name']:
                    image_id = {'id':pro_img['id']}
                    list_image_id_for_product.append(image_id)
        #print(f"list_image_id_for_product = {list_image_id_for_product}")
        return list_image_id_for_product                       

    def get_list_category_for_product(self, product):
        response = self.wcapi.get("products/categories")
        category_list_by_doc = [cat.strip().replace('"', '') for cat in product['Categorie'].split("/")]
        list_category_for_product = []
        for category in response.json():
            for cat in category_list_by_doc:
                #print(f"category['name'] = {category['name']}")
                #print(f"cat = {cat}")
                if category['name'] == cat:
                    id_category = {'id':category['id']}
                    list_category_for_product.append(id_category)
        return list_category_for_product

    def update_data_list_cat_product(self, list_category_id, list_img_id, product_id):
        product_data = {
            'categories':list_category_id,
            'images':list_img_id,
        }
        print(f"product_id = {product_id}")
        self.wcapi.put(f"products/{product_id}", product_data)
        #self.wcapi.get(f"products/{product_id}")

    def create_product(self, product):
        
        product_data = {
            'name' : product['Nom'],
            'price': product['Prix'],
            'stock_quantity': product['Stock'],
            'description': product['Description'],
            'short_description': product['Courte_description'],
        }
        response = self.wcapi.post("products/", product_data)
        if response.status_code == 201:
            product = response.json()
            return product["id"]
        else:
            return None
        
    def get_doc_ods(self):
        ezodf.config.set_table_expand_strategy('all')
        doc = ezodf.opendoc(self.filename_ods)
        sheet = doc.sheets[0]

        # Convertir la feuille en DataFrame
        data = []
        for row in sheet.rows():
            data.append([cell.value for cell in row])

        # Convertir en DataFrame Pandas
        df = pd.DataFrame(data)
        df.columns = df.iloc[0]
        df = df[1:].reset_index(drop=True)
        df = df.dropna(how='all')
        json_data = df.to_dict(orient="records")
        return json_data
        
    def update(self):
        json_data = self.get_doc_ods()
        for product in json_data:
            product_id = self.create_product(product)  
            list_category_id = self.get_list_category_for_product(product)
            list_img_id = self.get_list_id_img_for_product(product)
            self.update_data_list_cat_product(list_category_id, list_img_id, product_id)
            
    def delete_product(self):
        json_data = self.get_doc_ods()
        for product in json_data:
            list_products = self.wcapi.get(f"products/")
            for pro in list_products.json():
                print(f"product['Nom'] = {product['Nom']}")
                print(f"pro['name'] = {pro['name']}")
                if product['Nom'] == pro['name']:
                    self.wcapi.delete(f"products/{pro['id']}")
                    
            
    def delete_img_product(self):
        list_products = self.wcapi.get(f"products/")
        for pro in list_products.json():  
            #print(f"pro['name'] = {pro['name']}")  
            for img_pro in self.uploaded_images_pro:
                #print(f"img_pro['title'] = {img_pro['title']}")
                if pro['name'] == img_pro['title']:
                    #print(f"img_pro['id'] = {img_pro['id']}")
                    #print(f"pro['id'] = {pro['id']}")
                    response = requests.post(
                        f"{self.media_api_url}/{img_pro['id']}",
                        headers=self.headers,
                        json={"force": True},  # 🔥 Forcer la suppressio
                        verify=False 
                    )
                   
    
    def delete_all_img_product_by_name(self):
        response = requests.get(
            f"{self.media_api_url}?per_page=100",
            headers=self.headers,
            verify=False 
        )
        images = response.json()
        for img in images:    
            for path_image, name_image, title_image, text_alt, category_name in self.uploaded_images_pro:
                if img['title']['rendered'] == title_image:
                    print(f"img_id = {img['id']}")
                    print('iiii')
                    response = requests.delete(
                        f"{self.media_api_url}/{img['id']}?force=true",
                        headers=self.headers,
                        verify=False
                    )

                    print(f"Status Code: {response.status_code}")
                    pprint.pprint(response.json())
                    
    def delete_all_img_product(self):
        response = requests.get(
            f"{self.media_api_url}?per_page=100",
            headers=self.headers,
            verify=False 
        )
        images = response.json()
        for img in images:
            for img_pro in self.uploaded_images_pro:
                if img['title']['rendered'] == img_pro['title']:
                    print('coucou')
                    print(f"img_pro['id'] = {img_pro['id']}")
                    print(f"img['id'] = {img['id']}")
                    response = requests.delete(
                        #f"{self.media_api_url}/{img['id']}?force=true"
                        f"{self.media_api_url}/{img_pro['id']}?force=true",
                        headers=self.headers,
                        verify=False
                    )
                    print(f"Status Code: {response.status_code}")
                    pprint.pprint(response.json())


            
    def delete(self):
        #self.delete_product()
        #self.delete_img_product()
        #self.delete_all_img_product()
        self.delete_all_img_product_by_name()

url_website = "https://lescreationsdemissbleue.local"
filename_ods = "C:\\Users\\beren\\OneDrive\\Documents\\nextcloud\\beren\\site_missbleue\\infos_site.ods"

image_manager = ImageManager(images_to_upload,ath,url_website)
dict_image = image_manager.update()

#category_manager = CategoryManager(wcapi,ath,categories_to_create,url_website,dict_image['category'])
#category_manager.update()
#category_manager = CategoryManager(wcapi,ath,categories_to_create,url_website,images_to_upload[0]["categories"])
#category_manager.delete()

product_manager = ProductManager(wcapi,ath,filename_ods,dict_image['product'])
#product_manager.update()
#product_manager = ProductManager(wcapi,ath,filename_ods, images_to_upload[1]["products"])
product_manager.delete()

"""ALL_MEDIAS=[
    { 'slug': None, 'title': None, 'alt_text': None }
]

ALL_CATEGORIES=[
    { 'name': None, 'parent_name': 'None', 'description': None, 'media_slug': None }
]

ALL_PRODUCTS=[
    { 'name': None, 'parent_name': 'None', 'description': None, 'media_slug_list': [] }
]

api = WoocommerceApi(...)

media_manager = MediaManager(api=api)
media_manager.upload(media_list=...)
media_manager.find_id_by_slug(slug=...)
media_manager.delete(slug=...)

category_manager = CategoryManager(api=api, medias=media_manager.get_all_as_slug_dict())
category_manager.create(name, short_description, description, )


product_manager = ProductManager(api=api, medias=media_manager.get_all())
product_manager.create(product_list=...)

manager = WooCommerceManager(media_manager, category_manager, product_manager)

manager.delete_product(slug=...)


"""