import os
import grpc
import CBCManager_pb2, CBCManager_pb2_grpc
from CBCManager_pb2 import MovementType, ServiceForm

client_host = os.getenv("CLIENT_HOST", "cbc.grpc")
with open("ca.pem", "rb") as fp:
    ca_cert = fp.read()


the_channel = grpc.secure_channel(
    f"{client_host}:443", grpc.ssl_channel_credentials(ca_cert)
)
the_client = CBCManager_pb2_grpc.CBCManagerServiceStub(the_channel)

## GetUserAccounts
# req = CBCManager_pb2.UserAccountsRequest(
#     cbc_api_host="https://defense-prod05.conferdeploy.net",
#     auth_token="G6PL7F93SYA6AHHDY4IMNLQH/EZ82ITFI24",
#     org_key="N33FVJVQ",
# )
# res = the_client.GetUserAccounts(req)

## CreateUserAccount
# req = CBCManager_pb2.CreateUserAccountRequest(
#     org_id=31505,
#     cbc_api_host="https://defense-prod05.conferdeploy.net",
#     auth_token="G6PL7F93SYA6AHHDY4IMNLQH/EZ82ITFI24",
#     org_key="N33FVJVQ",
#     user_data=CBCManager_pb2.UserAccount(
#         contact_id=None,
#         email="nibel@protonmail.ch",
#         first_name="David",
#         last_name="Prueba",
#         login_id=None,
#         phone=None,
#     ),
# )
# res = the_client.CreateUserAccount(req)

## UpdateUserAccount
# req = CBCManager_pb2.UpdateUserAccountRequest(
#     user_data=CBCManager_pb2.UserAccount(
#         contact_id=15994279,
#         email="nibel@protonmail.ch",
#         first_name="David",
#         last_name="1234",
#         login_id=134091,
#         phone=None,
#     ),
#     org_id=31505,
#     login_name="nibel@protonmail.ch",
#     cbc_api_host="https://defense-prod05.conferdeploy.net",
#     auth_token="G6PL7F93SYA6AHHDY4IMNLQH/EZ82ITFI24",
#     org_key="N33FVJVQ",
# )
# res = the_client.UpdateUserAccount(req)

## DeleteUserAccount
# req = CBCManager_pb2.DeleteUserAccountRequest(
#     login_id=134091,
#     cbc_api_host="https://defense-prod05.conferdeploy.net",
#     auth_token="G6PL7F93SYA6AHHDY4IMNLQH/EZ82ITFI24",
#     org_key="N33FVJVQ"
# )
# res = the_client.DeleteUserAccount(req)

## RetrieveVMs
# req = CBCManager_pb2.RetrieveVMsRequest(
#     vdc_id="b8bf1580-a7e3-461c-8991-24c81bb1a9d5",
#     vcd_api_host="https://ntcvvcd-stg.telmex.com",
#     auth="david.estrada@SGM_STG:A#el961M2VY2fgk5",  # Credenciales en formato básico o bearer token
#     # auth="david.estrada@SGM_STG:A#el961M2VY2fgk5",  # Credenciales en formato básico o bearer token
#     is_basic_auth=True,
#     api_version="36.0",
#     installed_sensors=[]    # Arreglo de hrefs de sensores instalados recuperados de la base de datos propia
# )
# res = the_client.RetrieveVMs(req)

## RetrieveWorkloadSensors
# req = CBCManager_pb2.RetrieveSensorsRequest(
#     cbc_api_host="https://defense-prod05.conferdeploy.net",
#     auth_token="G6PL7F93SYA6AHHDY4IMNLQH/EZ82ITFI24",
#     # device_id = 103566215
#     device_id=0,
# )
# res = the_client.RetrieveWorkloadSensors(req)

## SensorManage
# req = CBCManager_pb2.SensorManagementRequest(license_form=CBCManager_pb2.LicenseForm(orgVdc="OrgVDC_SGMSTG", movementType=MovementType.INSTALL, hrefVM="http://test.com", licenseId=0, requestsQueueId=123))

# res = the_client.SensorManage(req)

req = CBCManager_pb2.ServiceManagementRequest(service_form=
    ServiceForm(
        service="Next Gen Antivirus",
        movementType=MovementType.CONTRACTING,
        plan="Next Gen Antivirus Empresarial",
        orgVdc="OrgVDC_SGMSTG",
        paymentMethod="Cobro único/anual",
        licenses=5,
        expireDate="N/A",
        requestQueueId=156,
    )
)

res = the_client.ServiceManage(req)
print(res)
