# sync/management/commands/sync_woo_products.py
from django.core.management.base import BaseCommand
from project.utils.woocommerce_service import get_woocommerce_client
from project.models import Bouquet, BouquetVariant

class Command(BaseCommand):
    help = "Sync WooCommerce products into Django Bouquets"
    products = wcapi.get("products", params={"per_page": 100, "page": page}).json()
    for p in products:
    sync_product_with_variants(wcapi, p)

    # def handle(self, *args, **options):
    #     wcapi = get_woocommerce_client()
    #     products = wcapi.get("products").json()
    #     self.stdout.write(f"Fetched {len(products)} products from WooCommerce")

    #     for p in products:
    #         bouquet, created = Bouquet.objects.update_or_create(
    #             woo_product_id=p['id'],
    #             defaults={
    #                 'name': p['name'],
    #                 'sku': p['sku'] or f"woo-{p['id']}",
    #                 'active': p['status'] == 'publish',
    #             }
    #         )
    #         if created:
    #             self.stdout.write(self.style.SUCCESS(f"Created new bouquet: {bouquet.name}"))
    #         else:
    #             self.stdout.write(self.style.WARNING(f"Updated bouquet: {bouquet.name}"))

    def handle(self, *args, **options):
        wcapi = get_woocommerce_client()
        page = 1
        total_synced = 0

        while True:
            products = wcapi.get("products", params={"per_page": 100, "page": page}).json()
            if not products:  # no more products
                break

            for p in products:
                bouquet, created = Bouquet.objects.update_or_create(
                    woo_product_id=p['id'],
                    defaults={
                        'name': p['name'],
                        'sku': p['sku'] or f"woo-{p['id']}",
                        'active': p['status'] == 'publish',
                    }
                )
                total_synced += 1
            page += 1

        self.stdout.write(self.style.SUCCESS(f"Synced {total_synced} bouquets from Woo"))



    def sync_product_with_variants(wcapi, product):
        bouquet, _ = Bouquet.objects.update_or_create(
            woo_product_id=product['id'],
            defaults={
                'name': product['name'],
                'sku': product['sku'] or f"woo-{product['id']}",
                'active': product['status'] == 'publish',
            }
        )

        # Get variations for this product
        variations = wcapi.get(f"products/{product['id']}/variations", params={"per_page": 100}).json()
        for v in variations:
            size_attr = None
            for attr in v['attributes']:
                if attr['name'].lower() == "size":
                    size_attr = attr['option']
            if not size_attr:
                continue

            BouquetVariant.objects.update_or_create(
                woo_variation_id=v['id'],
                bouquet=bouquet,
                defaults={
                    'size': size_attr.capitalize(),  # "Small", "Medium", etc.
                    'price': v.get('price'),
                }
            )
