<?php

namespace App\Avanse\Processes;

class AvanseCreditPolicy extends PolicyChecker
{
    private $cycle = [4 ,7, 9];
    private $cycleDate;
    private $interest = 0;
    private $pf = 0;
    protected $employmentVerification = true;
    protected $residenceVerification = true;

    public function isWorthy(){
return true;
        if ($this->request->has('cibil_error')) {
            $this->errorBag['CIBIL_ERROR'] = true;
            return false;
        }

        $this->loadAdditionalData();
        if ($response = $this->sectionOne()){
            return $this->saveWorthiness($response);
        }else if (!array_filter($this->errorBag) && $response = $this->sectionTwo()){
            return $this->saveWorthiness($response);
        }else if (!array_filter($this->errorBag) && $response = $this->sectionThree()){
            return $this->saveWorthiness($response);
        }

        return $this->saveWorthiness();
    }

    private function sectionOne(){

    }

    private function sectionTwo(){

        return false;
        if (!$this->isGeoLimit()){
            $this->errorBag['GeoLimit'] = true;
            return false;
        }

        if ($this->request->get('loan_amount') >= 20000 && $this->request->get('loan_amount') <= 100000){
            if (!in_array($this->request->get('employee_status'), ['SALARIED', 'Self Employed - Professional', 'Self Employed - Business'])){
                $this->errorBag['Profile'] = true;
                return false;
            }

            if ($this->request->get('loan_amount') <= 50000){
                $this->tenorCriteria = 12;
            }else{
                $this->tenorCriteria = 18;
            }

            if ($this->request->get('tenure') && $this->request->get('tenure') > $this->tenorCriteria){
                $this->errorBag['Tenure'] = true;
                return false;
            }

            $this->cycleDate = $this->cycle;

            if ($this->request->get('employee_status') == 'SALARIED' && $this->getAge() >= 21 && $this->getAgeAtLastEMI() <= 60){

                if ($this->request->get('cibil_score')['max'] >= 700 && $this->request->get('monthly_income') < 15000){
                    $this->errorBag['Income'] = false;
                    return false;
                }else if ($this->request->get('cibil_score')['max'] < 700 && $this->request->get('monthly_income') < 20000 && 'NTC'){
                    $this->errorBag['Income'] = false;
                    return false;
                }else if(!$this->request->get('monthly_income')){
                    //check basis foir
                }

            }elseif (in_array($this->request->get('employee_status'), ['Self Employed - Professional', 'Self Employed - Business']) && $this->getAge() >= 21 && $this->getAgeAtLastEMI() <= 65){

                if ($this->request->get('cibil_score')['max'] >= 700 && $this->request->get('average_credit') < 15000){
                    $this->errorBag['Income'] = false;
                    return false;
                }else if ($this->request->get('cibil_score')['max'] < 700 && $this->request->get('average_credit') < 20000 && 'NTC'){
                    $this->errorBag['Income'] = false;
                    return false;
                }
            }else{
                $this->ageMismatch = true;
            }

            if (!$this->isPANValid()){
                $this->errorBag['PAN'] = true;
                return false;
            }

            if (!$this->salaryCredited()){
                $this->errorBag['EmploymentStability'] = true;
                return false;
            }

            $this->employmentVerification = false;
            if ($this->residenceVerification && !$this->residenceVerification()) {
                $this->errorBag['ResidenceVerification'] = true;
                return false;
            }

            if (!$this->isBanking()){
                return true;
            }

            if (!$this->CibilValidations()){
                return false;
            }

            $this->setApprovalResponse();

            return true;
        }

        return false;
    }

    private function sectionThree(){
        if ($this->request->get('loan_amount') > 40000 && $this->request->get('loan_amount') <= 500000){
            if ($this->request->get('loan_amount') >= 40000 && $this->request->get('loan_amount') <= 200000){
                $this->tenorCriteria = 30;
            }elseif ($this->request->get('loan_amount') >= 200000 && $this->request->get('loan_amount') <= 400000){
                $this->tenorCriteria = 42;
            }elseif ($this->request->get('loan_amount') >= 400000 && $this->request->get('loan_amount') <= 500000){
                $this->tenorCriteria = 48;
            }

            if (!$this->request->get('user_verified')){
                $this->errorBag['UNVERIFIED'] = 'User number is not verified';
                return false;
            }

            if ($this->request->get('tenure') && $this->request->get('tenure') > $this->tenorCriteria){
                $this->errorBag['Tenure'] = true;
                return false;
            }

            if ($this->request->get('employee_status') == 'SALARIED' && ($this->getAge() < 21 || $this->getAgeAtLastEMI() > 60)){
                $this->errorBag['Age'] = true;
                return false;
            }elseif (in_array($this->request->get('employee_status'), ['Self Employed - Professional', 'Self Employed - Business']) && ($this->getAge() < 21 || $this->getAgeAtLastEMI() > 65)){
                $this->errorBag['Age'] = true;
                return false;
            }


            if ($this->request->get('is_student')){
                if (!in_array(strtolower($this->request->get('co_applicant_relationship')), array_map('strtolower', ['spouse', 'wife', 'mother', 'father', 'brother', 'parents In-Laws', 'brother in law', 'sister-in-law']))){
                    $this->errorBag['Profile'] = 'Co-Applicant Relationship does not match';
                    return false;
                }
            }else if ($this->request->get('co_applicant_name')){
                //$this->errorBag['CoApplicantNotRequired'] = true;
                //return false;
            }

            if ($this->request->get('loan_amount') <= 200000 && $this->request->get('monthly_income') < 15000){
                $this->errorBag['Income'] = true;
                return false;
            }else if ($this->request->get('loan_amount') > 200000 && $this->request->get('monthly_income') < 25000){
                $this->errorBag['Income'] = true;
                return false;
            }

            if (!$this->isFoir()){
                $this->errorBag['FOIR'] = true;
                return false;
            }

            if ($this->request->get('loan_amount') <= 200000 && $this->request->get('cibil_score')['max'] < 650){
                if (!$this->checkABB()){
                    $this->errorBag['ABB'] = true;
                    return false;
                }
                //$this->errorBag['Score'] = true;
                //return false;
            }else if ($this->request->get('loan_amount') > 200000 && $this->request->get('cibil_score')['max'] < 700){
                if (!$this->checkABB()){
                    $this->errorBag['ABB'] = true;
                    return false;
                }
                //$this->errorBag['Score'] = true;
                //return false;
            }else{
                if (!$this->checkABB()){
                    $this->errorBag['ABB'] = true;
                    return false;
                }
            }

            if ($this->request->get('loan_amount') <= 200000 && $this->request->get('experience_in_month') < 6){ //experience in month
                $this->errorBag['Experience'] = 'Minimum work experience of 6 months and cases where experience is less than 6 months should be reviewed by Credit';
                return false;
            }else if ($this->request->get('loan_amount') > 200000 && $this->request->get('experience_in_month') < 9){ //experience in month
                $this->errorBag['Experience'] = 'Minimum work experience 9 months required';
                return false;
            }

            $this->setApprovalResponse();

            return true;
        }else{
            $this->errorBag['LOANAMOUNT'] = 'Loan amount should be > 1L & <= 5L';
            return false;
        }
    }

    private function saveWorthiness($response = array()){

        if ($response){

            return $response;
        }

        return false;
    }

}
