"""Minimal Python client for the Belgian Company Data API."""

import requests


class CompanyDataApi:

    def __init__(self, api_key, base_url='https://your-domain.example', timeout=10):
        self.api_key = api_key
        self.base_url = base_url.rstrip('/')
        self.timeout = timeout

    def _get(self, path, **params):
        response = requests.get(
            '%s%s' % (self.base_url, path),
            headers={'X-API-Key': self.api_key},
            params={k: v for k, v in params.items() if v is not None},
            timeout=self.timeout,
        )
        response.raise_for_status()
        return response.json()

    def get_company(self, enterprise_number, lang='NL', establishments=False):
        return self._get('/api/v1/companies/%s' % enterprise_number, lang=lang,
                         establishments='1' if establishments else None)

    def search(self, name=None, nace=None, city=None, zip=None, legal_form=None, status=None, limit=20, lang='NL'):
        return self._get('/api/v1/companies', name=name, nace=nace, city=city, zip=zip,
                         legal_form=legal_form, status=status, limit=limit, lang=lang)

    def changes(self, since, limit=100, offset=0, lang='NL'):
        return self._get('/api/v1/changes', since=since, limit=limit, offset=offset, lang=lang)

    def history(self, enterprise_number, limit=100):
        return self._get('/api/v1/companies/%s/history' % enterprise_number, limit=limit)
