<?php

namespace App\Http\Controllers\Admin;

use App\ABFLDocuments;
use App\ABFLDrawdownWrapper;
use App\ABFLKYC;
use App\ABFLSanctionWrapper;
use App\APIWrapper\Wrapper;
use App\ConsumerApplicationFiles;
use App\Http\Controllers\Controller;
use App\Method\ABFL\ABFLMethods;
use Illuminate\Http\Request;
use App\ABFLBreDecision;
use App\StatusLogs;

class ABFLController extends Controller{

    private $request;

    public function __construct(Request $request){

        $this->request = $request;
    }

    public function checkWorthiness(){

        $wrapper = new Wrapper();
        $wrapperMethods = $wrapper->register(ABFLMethods::class);
        $wrapper->payload->addPayload(new Request(['id' => $this->request->get('id')]));

        $ABFLBreDecision = ABFLBreDecision::where('account_id', $this->request->get('id'))->first(['id']);
        if (!isset($ABFLBreDecision->id)) {
            $wrapperRes = $wrapperMethods->decisionEngine($wrapper);
            if (!$wrapperRes->errorBag)
                return response()->json(['status' => 'OK', 'data' => ['Message' => 'Loan application sent to ABFL for decision']]);

            return response()->json(['status' => 'FAIL', 'data' => array_filter($wrapperRes->errorBag)]);
        }


        $ABFLKYC = ABFLKYC::where('application_id', $this->request->get('id'))->first(['id']);
        $status = StatusLogs::where('application_id', $this->request->get('id'))->where('status', 'disbursement checklist done')->first(['id']);
        if(isset($status->id)){
            if (!isset($ABFLKYC->id)) {
                $wrapperRes = $wrapperMethods->DKYC($wrapper);
                if ($wrapperRes->errorBag) {
                    return response()->json(['status' => 'FAIL', 'data' => array_filter($wrapperRes->errorBag)]);
                }
            }

            $wrapperRes = $wrapperMethods->updateCCC($wrapper);
            if ($wrapperRes->errorBag)
                return response()->json(['status' => 'FAIL', 'data' => array_filter($wrapperRes->errorBag)]);

            return $this->sanctionWrapper($wrapperMethods, $wrapper);
        }else{
            return response()->json(['status' => 'FAIL', 'data' => array_filter(['Error'=>'Running Api on Wrong stage'])]);
        }
    }

    public function sanctionWrapper($wrapperMethods, $wrapper){

        $ABFLSanctionWrapper = ABFLSanctionWrapper::where('application_id', $this->request->get('id'))->first(['id']);

        if (!isset($ABFLSanctionWrapper->id)) {
            $wrapperRes = $wrapperMethods->createAccountv2($wrapper);
            if ($wrapperRes->errorBag) {
                return response()->json(['status' => 'FAIL', 'data' => array_filter($wrapperRes->errorBag)]);
            }
        }

        $ABFLDrawdownWrapper = ABFLDrawdownWrapper::where('application_id', $this->request->get('id'))->first(['id']);
        if (!isset($ABFLDrawdownWrapper->id)) {
            $wrapperRes = $wrapperMethods->captureChargev2($wrapper);
            if ($wrapperRes->errorBag) {
                return response()->json(['status' => 'FAIL', 'data' => array_filter($wrapperRes->errorBag)]);
            }
        }

        $application = ConsumerApplicationFiles::where('consumer_application_id', $this->request->get('id'))->where('file_type', 'loan_agreement_esign')->first(['id']);

        if (!isset($application->id)){
            return response()->json(['status' => 'FAIL', 'data' => ['Message' => 'Loan agreement signing is pending']]);
        }

        $this->request->merge(['file_id' => $application->id, 'id' => $this->request->get('id')]);
        $uploadDocument =  $this->sendDocumentToABFL();
        $uploadDocumentArg = $uploadDocument->getData();

        if ($uploadDocumentArg->status == 'OK'){
            return response()->json(['status' => 'OK', 'data' => ['Message' => 'DrawDownWrapper triggered']]);
        }

        return $uploadDocument;
    }

    public function sendDocumentToABFL(){

        $wrapper = new Wrapper();
        $wrapperMethods = $wrapper->register(ABFLMethods::class);
        $wrapper->payload->addPayload($this->request);

        $wrapperRes = $wrapperMethods->uploadDocumentBase64($wrapper);
        if (!$wrapperRes->errorBag) {
            return response()->json(['status' => 'OK', 'message' => 'Upload Document triggered']);
        }
        return response()->json(['status' => 'FAIL', 'message' => $wrapperRes->errorBag['Error']]);
    }

    public function getDocumentMapping(){

        $mappedDocumentExists = [];

        if(!$this->request->has('file_type')) {
            $files = ConsumerApplicationFiles::where('consumer_application_id', $this->request->get('id'))->get(['id', 'file_type']);

            foreach ($files as $file) {
                $filesExistIds[$file->file_type][] = $file->id;
                $mappedDocumentExists[$file->file_type] = $file->file_type;
            }
        }else{
            $filesExistIds[$this->request->get('file_type')][] = $this->request->get('file_id');
            $mappedDocumentExists[$this->request->get('file_type')] = $this->request->get('file_id');
        }

        $mappedDocumentAvExists = [];

        if (isset($filesExistIds) && $filesExistIds) {
            foreach ($filesExistIds as $mainKey => $filesExistId) {
                $avansePushed = ABFLDocuments::where('application_id', $this->request->get('id'))->whereIn('consumer_application_file_id', $filesExistId)->first();
                if (isset($avansePushed->id)) {
                    $mappedDocumentAvExists[$mainKey] = $mainKey;
                }
            }
        }

        return [$mappedDocumentExists, $mappedDocumentAvExists];
    }
}
