import os
import re
#import magic
from dotenv import load_dotenv

load_dotenv()
MAX_CONTENT_LENGTH = os.getenv('MAX_CONTENT_LENGTH')


class Validation:
    def __init__(self):
        self.test = 's'

    def mobile_validation(mobile):
        try:
            pattern = r'^[6-9]\d{9}$'
            valid_mobile = re.match(pattern, mobile)
            if valid_mobile:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid mobile number.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid mobile parameter.'}

    def otp_validation(otp):
        try:
            pattern = r'^[0-9]{6}$'
            # matches exact 6 numeric digits
            valid_otp = re.match(pattern, otp)
            if valid_otp:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid OTP.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid otp parameter.'}

    def aadhaar_validation(aadhaar):
        try:
            pattern = r'^[2-9]{1}[0-9]{11}$'
            valid_aadhaar = re.match(pattern, aadhaar)
            if valid_aadhaar:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid Aadhaar number.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid aadhaar parameter.'}
    
    def name_validation(name):
        try:
            pattern = r'^[a-zA-Z]{4,}(?: [a-zA-Z]+){0,2}$'
            # min 4 char is required (accepts in form of: Fname Mname Lname)
            valid_name = re.match(pattern, name)
            if valid_name:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid name.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid name parameter.'}
    
    def dob_validation(dob):
        try:
            #pattern = r'^(0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$'
            pattern = r'^[0-9]{8}$'
            # It accepts dob in format ddmmyyy including leap year also
            valid_dob = re.match(pattern, dob)
            if valid_dob:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid Date of Birth.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid dob parameter.'+str(e)}
    
    def gender_validation(gender):
        try:
            if gender in ['M', 'F']:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid gender.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid gender parameter.'}
    
    def relation_validation(relation):
        try:
            relation_data = ['Partner', 'name', 'Son', 'Daughter', 'Mother', 'Father', 'Sister', 'Brother', 'Mother-in-law', 'Sister-in-law', 'Uncle', 'Aunt', 'Cousin', 'Grand Father', 'Grand Mother', 'Others',
                            'partner', 'name', 'son', 'daughter', 'mother', 'father', 'sister', 'brother', 'mother-in-law', 'sister-in-law', 'uncle', 'aunt', 'cousin', 'grand father', 'grand mother', 'others']
            
            if relation in relation_data:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid relation.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid relation parameter.'}
    
    def pin_validation(pin):
        try:
            pattern = r'^[0-9]{6}$'
            valid_otp = re.match(pattern, pin)
            if valid_otp:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid PIN.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid pin parameter.'}

    def email_validation(email):
        try:
            pattern = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'  
            valid_email = re.match(pattern, email)
            if valid_email:
                return 200, {'status': 'success'}
            else:
                return 400, {'status': 'error', 'error_description': 'Please enter valid email.'}
        except Exception as e:
            return 400, {'status': 'error', 'error_description': 'Invalid email parameter.'}