import fudo.fudo as fd
from logging import getLogger

logger = getLogger(__name__)

def add_product_to_fudo(product_id: int, quantity: int, table_number: int, comment=None):
    """Add a product to Fudo system"""
    table = fd.get_table(table_number)
    if not table:
        logger.error(f"Error: Table {table_number} not found.")
        return None
    
    activeSale = fd.get_active_sale(table)
    if not activeSale:
        activeSale = fd.create_sale(table['id'])
        if not activeSale:
            logger.error(f"Error: Could not create sale for table {table_number}.")
            return None
    
    item = fd.create_item(product_id, quantity, activeSale['id'], comment)
    if not item:
        logger.error(f"Error: Could not create item for product {product_id}.")
        return None
    
    return item
