"""DecisionLayer API quickstart: create a consent arbitration case.

The only thing you need to change is API_KEY below. Create a key at
https://www.decisionlayer.ai/settings/api-keys

Then run:

    pip install requests
    python create_consent_case.py
"""

import requests

# 1. Paste your API key here (from /settings/api-keys).
API_KEY = "PASTE_YOUR_KEY_HERE"

# 2. Base URL of the DecisionLayer API. Use http://localhost:8000 locally.
BASE_URL = "https://www.decisionlayer.ai"

HEADERS = {"Authorization": f"Bearer {API_KEY}"}


def main() -> None:
    # Create a small sample attachment so the demo includes a contract file.
    with open("sample_contract.txt", "w") as handle:
        handle.write("Sample contract for the DecisionLayer consent demo.\n")

    # The fields below mirror the consent web form. Claimant details default
    # to the API key owner's account when omitted.
    data = {
        "question_for_arbitration": "Respondent kept a $3,500 deposit.",
        "financial_demand_usd": "3500.00",
        "other_relief": "Return of any project files.",
        "respondent_first_name": "Jordan",
        "respondent_last_name": "Chen",
        "respondent_email": "jordan.chen@example.com",
    }

    with open("sample_contract.txt", "rb") as contract:
        files = {
            "contract_files": ("sample_contract.txt", contract, "text/plain"),
        }
        response = requests.post(
            f"{BASE_URL}/api/v1/cases",
            headers=HEADERS,
            data=data,
            files=files,
            timeout=60,
        )

    if response.status_code != 201:
        print(f"Request failed ({response.status_code}):")
        print(response.json())
        raise SystemExit(1)

    result = response.json()
    case = result["case"]
    print(f"Created consent case: {case['id']}")
    print(f"Status: {case['status']}")
    print(f"Sign it here: {result['sign_url']}")

    # List your cases to confirm it shows up.
    listing = requests.get(f"{BASE_URL}/api/v1/cases", headers=HEADERS, timeout=60)
    listing.raise_for_status()
    print(f"You now have {len(listing.json())} consent case(s).")


if __name__ == "__main__":
    main()
