import json
import os
from .CloudApiBase import CloudApiBase
from ..PostReqestBase import SSNNameMatchPostRequest


class SSNNameMatch(CloudApiBase):

    def __init__(self, license=None):
        super().__init__("https://namessn.melissadata.net", "/v4/web/SSN/doLookup")
        self.license = license or os.environ.get("MD_LICENSE")
        self.parameters["format"] = "json"
        self.records = []
        self.post_body = None

        self.parameter_mappings = {
            "ssn": "ssn",
            "first_name": "first",
            "last_name": "last",
            "full_name": "full",
            "transmission_reference": "t"
        }

        # Initialize private backing fields
        for attr in self.parameter_mappings.keys():
            setattr(self, f"_{attr}", None)

    def _set_parameter(self, attr_name, value):
        """
        Helper method to set a private attribute and update the parameters dictionary.
        """
        setattr(self, f"_{attr_name}", value)
        self.parameters[self.parameter_mappings[attr_name]] = value

    # Properties
    @property
    def ssn(self):
        return self._ssn

    @ssn.setter
    def ssn(self, value):
        self._set_parameter("ssn", value)

    @property
    def first_name(self):
        return self._first_name

    @first_name.setter
    def first_name(self, value):
        self._set_parameter("first_name", value)

    @property
    def last_name(self):
        return self._last_name

    @last_name.setter
    def last_name(self, value):
        self._set_parameter("last_name", value)

    @property
    def full_name(self):
        return self._full_name

    @full_name.setter
    def full_name(self, value):
        self._set_parameter("full_name", value)

    @property
    def transmission_reference(self):
        return self._transmission_reference

    @transmission_reference.setter
    def transmission_reference(self, value):
        self._set_parameter("transmission_reference", value)

    # Setters
    def set_ssn(self, ssn):
        self._set_parameter("ssn", ssn)

    def set_first_name(self, first_name):
        self._set_parameter("first_name", first_name)

    def set_last_name(self, last_name):
        self._set_parameter("last_name", last_name)

    def set_full_name(self, full_name):
        self._set_parameter("full_name", full_name)

    def set_transmission_reference(self, transmission_reference):
        self._set_parameter("transmission_reference", transmission_reference)

    def set_post_body(self, post_body):
        self.post_body = post_body

    def set_value(self, parameter, value):
        parameter = parameter.strip() if parameter else None
        
        if parameter in self.parameter_mappings:
            parameter_key = self.parameter_mappings[parameter]
            
            property_name = next((key for key, val in self.parameter_mappings.items() 
                                  if val.lower() == parameter_key.lower()), None)
            
            if property_name:
                if hasattr(self, property_name):
                    setattr(self, property_name, value)
        else:
            # If not in derived class, go to base class implementation
            super().set_value(parameter, value)

    # Getters
    def get_ssn(self):
        return self.parameters.get("ssn", "")

    def get_first_name(self):
        return self.parameters.get("first", "")

    def get_last_name(self):
        return self.parameters.get("last", "")

    def get_full_name(self):
        return self.parameters.get("full", "")

    def get_transmission_reference(self):
        return self.parameters.get("t", "")

    def get_post_body(self):
        return self.post_body
    
    # Class Methods
    def add_record(self, request):
        self.records.append(request)
    
    def clear_records(self):
        self.records.clear()

    def post(self, response_type):
        """
        This synchronous function makes the batch request using the post body.

        Returns:
            A string or deserialized json response object.
        """

        if self.records and len(self.records) > 0:
            post_request = SSNNameMatchPostRequest(
                transmission_reference=self.get_transmission_reference(),
                records=self.records,
                customer_id=self.get_license()
            )
            response = self.send_post_request(post_request)
        else:
            response = self.send_post_request(self.post_body)
        return self.handle_response(response, response_type)


    def get_api_version(self):
        """
        Makes a synchronous getversion request and parses the response for the API version.

        Returns:
            The Cloud API version.
        """
        json_response = self.send_version_request()
        response_objectect = json.loads(json_response)
        version = response_objectect["Version"]
        return version
