from app.models import DocumentDetails, ConsumerApplication, EmiCalculation
from app.util import datetime_util


class DocumentDetailsAccessor:

    @classmethod
    def create_document_details(cls, consumer_file_id, application_id, doc_type, name, tenure, emi_date, emi_amount, lender_name,
               loan_amount, insurance_fee, advance_amount, processing_fee, rate_of_interest, permanent_address,
               applicant_signed_date, co_applicant_signed_date, applicant_location_latitude, applicant_location_longitude,
               co_applicant_location_latitude, co_applicant_location_longitude):
        time_now = datetime_util.get_now_ist_string()

        json_data = {
            'name': name,
            'tenure': tenure,
            'emi_date': emi_date,
            'emi_amount': emi_amount,
            'lender_name': lender_name,
            'loan_amount': int(loan_amount),
            'insurance_fee': insurance_fee,
            'advance_amount': advance_amount,
            'processing_fee': processing_fee,
            'rate_of_interest': float(rate_of_interest),
            'permanent_address': permanent_address,
            'applicant_signed_date': applicant_signed_date,
            'co_applicant_signed_date': co_applicant_signed_date,
            'applicant_location_latitude': str(applicant_location_latitude),
            'applicant_location_longitude': str(applicant_location_longitude),
            'co_applicant_location_latitude': co_applicant_location_latitude,
            'co_applicant_location_longitude': co_applicant_location_longitude
        }

        DocumentDetails.objects.create(
            consumer_file_id=consumer_file_id,
            application_id=application_id,
            doc_type=doc_type,
            json_data=json_data,
            created_by=107,
            created_at=time_now,
        )

    @classmethod
    def get_document_details_json(cls, application_id):
        return ConsumerApplication.objects.filter(id=application_id).select_related(
            'emicalculation',
            'consumerapplicationelement',
            'lender'
        ).first()
