import logging
import requests
from typing import Optional
from ..interfaces import CRMRepository, LeadData, TaskData
from ..config import settings

logger = logging.getLogger(__name__)

class SuperOfficeRepository(CRMRepository):
    def __init__(self, tenant_id: str, api_token: str):
        self.base_url = f"https://{tenant_id}.superoffice.com/api/v1"
        self.headers = {
            "Authorization": f"Bearer {api_token}",
            "Accept": "application/json"
        }

    def get_name(self) -> str:
        return "SuperOffice"

    def find_company(self, name: str, email: str = None) -> Optional[str]:
        # TODO: Implement actual OData query
        # Example: GET /Contact?$filter=Name eq '{name}'
        logger.info(f"[SuperOffice] Searching for '{name}'...")
        return None 

    def create_lead(self, lead: LeadData) -> str:
        logger.info(f"[SuperOffice] Creating Lead: {lead.name}")
        # TODO: POST /Contact
        # Payload mapping: lead.industry -> SuperOffice BusinessId
        return "SO_DUMMY_ID_123"

    def update_lead(self, external_id: str, lead: LeadData) -> bool:
        logger.info(f"[SuperOffice] Updating Lead {external_id} with Score {lead.robotics_potential_score}")
        # TODO: PUT /Contact/{id}
        # Wir schreiben das Robotics-Potential z.B. in ein benutzerdefiniertes Feld (UserDefinedField)
        return True

    def create_task(self, external_id: str, task: TaskData) -> bool:
        logger.info(f"[SuperOffice] Creating Task for {external_id}: {task.subject}")
        return True
