import json
import time

import requests


def test_api(limit):
    url = f'http://localhost:5000/api/agent/n8n/products?limit={limit}'
    print(f'\n--- Testing API with limit={limit} ---')
    print(f'URL: {url}')

    start_time = time.time()
    try:
        response = requests.get(url, timeout=10)
        elapsed = time.time() - start_time

        print(f'Status Code : {response.status_code}')
        print(f'Time Taken  : {elapsed:.3f} seconds')

        if response.status_code == 200:
            data = response.json()
            print(f'Status Field: {data.get("status")}')
            print(f'Total Field : {data.get("total")}')

            products = data.get("products", [])
            print(f'Products returned: {len(products)}')

            if len(products) > 0:
                print('\nSample Product (first item):')
                print(json.dumps(products[0], indent=2, ensure_ascii=False))
            else:
                print('WARNING: No products returned. Is the database empty or filter too strict?')
        else:
            print('ERROR Response:')
            try:
                print(json.dumps(response.json(), indent=2, ensure_ascii=False))
            except:
                print(response.text)

    except Exception as e:
        print(f'Request failed: {e}')

if __name__ == '__main__':
    test_api(3)
    test_api(10)
