import openapi_client
from openapi_client.rest import ApiException
from openapi_client.models.create_customer_request import CreateCustomerRequest
from openapi_client.models.update_customer_request import UpdateCustomerRequest
import os
import sys

def cmdline_options():
    print("Options:")
    print("   --post-customer=[id]")
    print("   --get-customer=[id]")
    print("   --delete-customer=[id]")
    print("   --patch-customer=[id]")
    print("   --get-restaurant-menu=[id]")
    print("   --get-restaurants")

def api():
    configuration = openapi_client.Configuration(
        host = "http://localhost:4004"
    )

    # The client must configure the authentication and authorization parameters
    # in accordance with the API server security policy.
    # Examples for each auth method are provided below, use the example that
    # satisfies your auth use case.

    # Configure HTTP basic authorization: BasicAuth
    configuration = openapi_client.Configuration(
        username = os.environ["USERNAME"],
        password = os.environ["PASSWORD"]
    )

    if len(sys.argv) <= 1:
        cmdline_options()
        return


    with openapi_client.ApiClient(configuration) as api_client:
        api_instance = openapi_client.CustomersApi(api_client)
        api_res_instance = openapi_client.RestaurantsApi(api_client)

        if "=" in sys.argv[1]:
            arg, id = sys.argv[1].split("=")
            match arg:
                case "--post-customer":
                    try:
                        print("Customer name:")
                        name = input()
                        print("Customer email:")
                        email = input()
                        result = api_instance.customers_post(CreateCustomerRequest(name=name, email=email), id)
                        print(result)
                    except ApiException as e:
                        print("Exception when calling CustomersApi->customers_post: %s\n" % e)
                case "--get-customer":
                    try:
                        result = api_instance.customers_id_get(id)
                        print(result)
                    except ApiException as e:
                        print("Exception when calling CustomersApi->customers_id_get: %s\n" % e)
                case "--delete-customer":
                    try:
                        result = api_instance.customers_id_delete(id)
                        print(result)
                    except ApiException as e:
                        print("Exception when calling CustomersApi->customers_id_delete: %s\n" % e)
                case "--patch-customer":
                    try:
                        print("Customer name:")
                        name = input()
                        print("Customer email:")
                        email = input()
                        result = api_instance.customers_id_patch(id, UpdateCustomerRequest(name=name, email=email))
                        print(result)
                    except ApiException as e:
                        print("Exception when calling CustomersApi->customers_id_patch: %s\n" % e)
                case "--get-restaurant-menu":
                    try:
                        result = api_res_instance.restaurants_id_menu_get(id)
                        print(result)
                    except ApiException as e:
                        print("Exception when calling RestaurantsApi->restaurants_id_menu_get: %s\n" % e)
                case _:
                    cmdline_options()
        else:
            arg = sys.argv[1]
            match arg:
                case "--get-restaurants":
                    try:
                        result = api_res_instance.restaurants_get()
                        print(result)
                    except ApiException as e:
                        print("Exception when calling RestaurantsApi->restaurants_get: %s\n" % e)
                case _:
                    cmdline_options()

if __name__ == "__main__":
    api()