from decimal import Decimal

from app.database import SessionLocal
from app.models.chargepoint import ChargePoint
from app.models.chargepoint_variable import ChargepointVariable

from app.schemas.chargepoint_variable import AttributeType, DataType, MutabilityType

async def create_or_update_variable(chargepoint_identity: str, report_entry):
    with SessionLocal() as db:
        db_chargepoint = db.query(ChargePoint).filter(ChargePoint.identity == chargepoint_identity).first()
        for variable_attribute in report_entry['variable_attribute']:
            query = db.query(ChargepointVariable).filter(
                ChargepointVariable.chargepoint_id == db_chargepoint.id,
                ChargepointVariable.component_name == report_entry['component']['name'],
                ChargepointVariable.name == report_entry['variable']['name']
            )
            if "instance" in report_entry['component'].keys():
                query = query.filter(ChargepointVariable.component_instance == report_entry['component']['instance'])
            if "evse" in report_entry['component'].keys():
                query = query.filter(ChargepointVariable.evse == report_entry['component']['evse']['id'])
                if "connectorId" in report_entry['component']['evse'].keys():
                    query = query.filter(ChargepointVariable.connector_id == report_entry['component']['evse']['connectorId'])
            if "type" in variable_attribute.keys():
                query = query.filter(ChargepointVariable.type == AttributeType(variable_attribute['type']))
            else:
                query = query.filter(ChargepointVariable.type == AttributeType.ACTUAL)
            db_variable = query.first()
            if db_variable == None:
                db_variable = ChargepointVariable()
                db_variable.chargepoint_id = db_chargepoint.id
                db_variable.component_name = report_entry['component']['name']
                db_variable.name = report_entry['variable']['name']

                if "value" in variable_attribute.keys():
                    db_variable.value = variable_attribute['value']
                if "instance" in report_entry['component'].keys():
                    db_variable.component_instance = report_entry['component']['instance']
                if "evse" in report_entry['component'].keys():
                    db_variable.evse = report_entry['component']['evse']['id']
                    if "connector_id" in report_entry['component']['evse'].keys():
                        db_variable.connector_id = report_entry['component']['evse']['connector_id']
                if "constant" in variable_attribute.keys():
                    db_variable.constant = variable_attribute['constant']
                if "persistent" in variable_attribute.keys():
                    db_variable.constant = variable_attribute['persistent']
                if "mutability" in variable_attribute.keys():
                    db_variable.mutability = MutabilityType(variable_attribute['mutability'])
                if "type" in variable_attribute.keys():
                    db_variable.type = AttributeType(variable_attribute['type'])
                if "variable_characteristics" in report_entry.keys():
                    db_variable.data_type = DataType(report_entry['variable_characteristics']['data_type'])
                    if "min_limit" in report_entry['variable_characteristics'].keys():
                        db_variable.min_limit = Decimal(report_entry['variable_characteristics']['min_limit'])
                    if "max_limit" in report_entry['variable_characteristics'].keys():
                        db_variable.max_limit = Decimal(report_entry['variable_characteristics']['max_limit'])
                    if "unit" in report_entry['variable_characteristics'].keys():
                        db_variable.unit = report_entry['variable_characteristics']['unit']
                    if "values_list" in report_entry['variable_characteristics'].keys():
                        db_variable.values_list = report_entry['variable_characteristics']['values_list']
                db.add(db_variable)
            else:
                if "value" in variable_attribute.keys():
                    db_variable.value = variable_attribute['value']
            db.commit()