import requests as http_requests
from django.conf import settings
import logging

logger = logging.getLogger(__name__)


class OdooService:
    def __init__(self):
        self.base_url = settings.ODOO_URL.rstrip('/')

    def get_products(self):
        """Получить список всех товаров из Odoo"""
        try:
            url = f"{self.base_url}/fastapi/products/products"
            response = http_requests.get(url, timeout=10)
            if response.status_code == 200:
                return response.json()
            else:
                logger.error(f"Error fetching products: {response.status_code}")
                return []
        except Exception as e:
            logger.error(f"Error fetching products from Odoo: {e}")
            return []

    def get_logistics_nodes(self):
        """Получить список всех логистических узлов из Odoo"""
        try:
            url = f"{self.base_url}/fastapi/logistics/nodes"
            response = http_requests.get(url, timeout=10)
            if response.status_code == 200:
                return response.json()
            else:
                logger.error(f"Error fetching logistics nodes: {response.status_code}")
                return []
        except Exception as e:
            logger.error(f"Error fetching logistics nodes from Odoo: {e}")
            return []
