#!/usr/bin/env python3
import urllib.request
import json
import sys

def test_create_policy():
    data = json.dumps({
        'customer_id': 'cus_01JEMN4XZ8Y6Q3KPVWRST2FGHA',
        'product_type': 'health',
        'effective_date': '2025-12-03T00:00:00Z',
        'expiration_date': '2026-12-03T00:00:00Z',
        'premium_amount': 500.00,
        'status': 'active',
        'currency': 'USD',
        'billing_frequency': 'monthly',
        'coverage_details': {'deductible': 1000, 'max_coverage': 100000},
        'tags': ['health', 'individual', 'premium']
    }).encode('utf-8')

    req = urllib.request.Request(
        'http://localhost:8080/api/v1/policies',
        data=data,
        headers={'Content-Type': 'application/json'},
        method='POST'
    )

    try:
        with urllib.request.urlopen(req, timeout=10) as response:
            print(f'Status: {response.status}')
            body = response.read().decode('utf-8')
            result = json.loads(body)
            print(f'Policy ID: {result.get("id")}')
            print(f'Policy Number: {result.get("policy_number")}')
            print(json.dumps(result, indent=2))
            return 0
    except urllib.error.HTTPError as e:
        print(f'HTTP Error {e.code}')
        print(e.read().decode())
        return 1
    except Exception as e:
        print(f'Error: {e}')
        return 1

if __name__ == '__main__':
    sys.exit(test_create_policy())
