#!/opt/homebrew/bin/python3.11
"""
Test face registration API endpoint
"""

import requests
import json
import os
import sys

# API configuration
API_URL = "http://localhost:3002/api/v1/face/register"
API_KEY = "muser_243c6725b09f43e29f319a648645b992_1774874668_f224a6d2"

# Test image path
TEST_IMAGE = "/tmp/face_analysis_results/384b0ff44aaaa1f1_frame_019778.jpg"


def test_face_registration():
    """Test face registration API endpoint"""

    if not os.path.exists(TEST_IMAGE):
        print(f"Test image not found: {TEST_IMAGE}")
        return False

    print(f"Testing face registration with image: {TEST_IMAGE}")

    # Prepare multipart form data
    files = {"image": ("test_face.jpg", open(TEST_IMAGE, "rb"), "image/jpeg")}

    data = {
        "name": "Test Person API",
        "metadata": json.dumps(
            {"source": "test_api", "notes": "Test registration via API"}
        ),
    }

    headers = {"X-API-Key": API_KEY}

    try:
        response = requests.post(
            API_URL, files=files, data=data, headers=headers, timeout=30
        )

        print(f"Status Code: {response.status_code}")
        print(f"Response Headers: {dict(response.headers)}")

        if response.status_code == 200:
            result = response.json()
            print(f"Success! Response: {json.dumps(result, indent=2)}")
            return True
        else:
            print(f"Error! Response: {response.text}")
            return False

    except Exception as e:
        print(f"Exception during API call: {e}")
        import traceback

        traceback.print_exc()
        return False


if __name__ == "__main__":
    success = test_face_registration()
    sys.exit(0 if success else 1)
