<?php

namespace App\Http\Controllers;

use Hashids\Hashids;
use App\CommunicationMapping;
use App\CommunicationService;
use App\Merchant;
use App\Shortio;
use Carbon\Carbon;
use App\Http\Controllers\Admin\ConsumerDocument;
use App\ConsumerApplication;
use App\ConsumerApplicationElement;
use App\Templates;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use App\BNPL;
use App\ConsentLog;
use App\ConsumerApplicationFiles;
use App\Logging\CloudWatchLoggerFactory;

class ConsentController extends Controller
{

    public function showConsentPage($hashed_id, $applicant_type = 1)
    {
        $hashids = new Hashids('Creditfair by CS', 20);
        $decoded = $hashids->decode($hashed_id);

        if (empty($decoded)) {
            abort(404, 'Invalid link');
        }

        $id = $decoded[0];

        if ($applicant_type == 2) {
            $application = DB::connection('mysql_primary')
                ->table('consumer_application')
                ->where('id', $id)
                ->first(['id', 'co_applicant_name as name', 'merchant_id']);
        } else {
            $application = DB::connection('mysql_primary')
                ->table('consumer_application')
                ->where('id', $id)
                ->first(['id', 'name', 'merchant_id']);
        }

        if (!$application) {
            abort(404, 'Application not found');
        }
        $doc_name = $applicant_type == 1 ? 'consent_logs' : 'co_app_consent_logs';
        $merchant = DB::table('merchant')->where('id', $application->merchant_id)->first(['merchant_name']);
        $consumerApplicationElement = ConsumerApplicationElement::where('application_id', $id)
            ->first(['is_consent', 'is_co_app_consent']);
        $consumerApplicationFiles = ConsumerApplicationFiles::where('consumer_application_id', $id)
            ->where('applicant_type', $applicant_type)
            ->where('file_type', $doc_name)
            ->whereNull('deleted_at')
            ->first(['id']);

        $alreadyLogged = $applicant_type == 1 ? $consumerApplicationElement->is_consent : $consumerApplicationElement->is_co_app_consent;
        if ($consumerApplicationElement && $consumerApplicationFiles && $consumerApplicationFiles->id && $alreadyLogged) {
            $message = 'We have already logged your Consent';
            $title = 'Thank You!';
            return view('front.message-redirect', ['message' => $message, 'title' => $title]);
        }

        return view('front.consent_page', [
            'name' => $application->name,
            'merchant_name' => $merchant->merchant_name ?? '',
            'application_id' => $hashed_id,
            'applicant_type' => $applicant_type,
        ]);
    }

    public function sendConsentMessage($id, $applicant_type = 1)
    {
        $hashids = new Hashids('Creditfair by CS', 20);
        $hashed_id = $hashids->encode($id);
        $consent_path = $hashed_id;
        
        if ($applicant_type == 2) {
            $consent_path .= "/2";
            $application = DB::connection('mysql_primary')
                ->table('consumer_application')
                ->where('id', $id)
                ->first(['id', 'co_applicant_name as name', 'co_applicant_email as email', 'co_applicant_mobile_number as mobile_number', 'merchant_id']);
        } else {
            $application = DB::connection('mysql_primary')
                ->table('consumer_application')
                ->where('id', $id)
                ->first(['id', 'name', 'email', 'mobile_number', 'merchant_id']);
        }

        if ($application->merchant_id && !BNPL::is($application->merchant_id)) {
            $email = isset($application->email) ? wall_decrypt($application->email) : null;
            $mobile_number = isset($application->mobile_number) ? wall_decrypt($application->mobile_number) : null;
            $merchant = Merchant::where('id', $application->merchant_id)->first(['merchant_name']);
            $consent_key = array_search('Consent', CommunicationService::$com_category);
            $email_comm = CommunicationMapping::whereRaw('FIND_IN_SET('.$consent_key.',type_ids)')
                ->where('communication_id', 1)
                ->where('merchant_id', $application->merchant_id)
                ->count();
            $communication = CommunicationMapping::where('merchant_id', $application->merchant_id)->count();

            if (isset($email) && ($email_comm == 1 || $communication == 0)) {
                $body = '
                    <div style="font-size:13px;font-family:verdana,arial;line-height:1.5">
                        Dear ' . $application->name . ',
                            By clicking "I Accept", I hereby declare all information provided by me is correct and hereby consent to K M Global Credit Private Limited and its <a href="' . asset('affiliate-partners') . '">affiliate lending partners</a> to receive my bureau details and other financial /KYC details for financing purpose for purchase made with ' . $merchant->merchant_name . '
                        <div>Annual household Income > 3L</div>
                        <table style="width:100%">
                            <tbody>
                                <tr>
                                    <td align="center" style="padding:30px 30px 30px 30px">
                                        <table cellpadding="0" cellspacing="0" border="0">
                                            <tbody>
                                                <tr>
                                                    <td align="center" style="border-radius:3px;background-color:#0055cc;padding:11px 26px">
                                                        <a href="' . route('consent.page', ['hashed_id' => $hashed_id, 'applicant_type' => $applicant_type]) . '" style="color:white;text-decoration:none;text-transform:uppercase;font-size:16px" target="_blank">
                                                        I Agree</a>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>';

                if ($application->merchant_id != _env("Finsall_id")) {
                    Mail::send('emails.reminders.basic_reminder',
                        ['body' => $body],
                        function ($message) use ($email) {
                            $message->to([$email])->subject('IMPORTANT: Consent required');
                        });
                }
            }

            $link = Shortio::getUrl(asset("consent-page/" . $consent_path));
            $merchant_name = $merchant->merchant_name;
            $affiliateLink = "https://lnk.creditfair.in/5jES";
            $sms_body = "By clicking on link ".$link." I hereby declare all data provided by me is correct and hereby consent to K.M.Global Credit Pvt Ltd. and its affiliate lending partners ".$affiliateLink." to receive my bureau and other financial /KYC details for financing purpose for purchase made with ".$merchant_name.". Regards, Team Credit fair (KM Global)";
            $args = [$link, $merchant_name];
            $entity = ['templateid' => '1307173331891592692', 'entityid' => Templates::$entityid];
            
            send_sms($mobile_number, [
                'message' => $sms_body,
                'id' => $application->id,
                'id_type' => 'application_id',
                'templateDetails' => $entity,
                'args' => $args
            ]);

            $WA_comm = CommunicationMapping::whereRaw('FIND_IN_SET("'.$consent_key.'",type_ids)')
                ->where('communication_id', 3)
                ->where('merchant_id', $application->merchant_id)
                ->count();

            if ($mobile_number && ($WA_comm == 1 || $communication == 0)) {
                sendWhatsAppMessage($mobile_number, [
                    'id' => $id,
                    'templateId' => Templates::$consent_bureau_pull_v1,
                    'args' => [$merchant->merchant_name, $consent_path],
                    'templateType' => 'buttonTemplate'
                ]);
            }
        }
    }

    public function addConstantDocument($application_id, $applicant_type = 1, $show_view = true, $status , $request){
        $hashids = new Hashids('Creditfair by CS', 20);
        $hashed_id = $hashids->decode($application_id);
        $message = 'Please try after sometime';
        $title = 'Sorry!';

        if (isset($hashed_id[0])) {
            $application_id_decoded = $hashed_id[0];
            $fileName = 'consent_logs';
            $coFileName = 'co_app_consent_logs';

            if($status === 'deny') {
                $fileName = 'deny_consent_logs';
                $coFileName = 'deny_co_app_consent_logs';
            }
            $field_map = [
                1 => ['consent_flag' => 'is_consent', 'mobile_field' => 'mobile_number', 'name_field' => 'name', 's3_folder' => $fileName],
                2 => ['consent_flag' => 'is_co_app_consent', 'mobile_field' => 'co_applicant_mobile_number', 'name_field' => 'co_applicant_name', 's3_folder' => $coFileName]
            ];

            $config = $field_map[$applicant_type];

            $consumerApplicationElement = ConsumerApplicationElement::where('application_id', $application_id_decoded)
                ->first([$config['consent_flag']]);

            if (isset($consumerApplicationElement->{$config['consent_flag']}) && !$consumerApplicationElement->{$config['consent_flag']}) {
                try {
                    $application = ConsumerApplication::with('proFormaInvoice')
                        ->where('id', $application_id_decoded)
                        ->first(['id', 'name', 'co_applicant_name', 'mobile_number', 'co_applicant_mobile_number', 'lender_id']);

                    if ($application) {
                        $mobile = '';
                        $ip = '';
                        $ip = (new ApiController())->get_client_ip();
                        $mobile = $application->mobile_number;

                        $consentExists = ConsentLog::where('application_id', $application_id_decoded)
                            ->where('applicant_type', $applicant_type)
                            ->first(['id', 'latitude', 'longitude']);

                        $data = [
                            'loan_application_no' => $application->proFormaInvoice && isset($application->proFormaInvoice->loan_application_no)
                                ? ucwords($application->proFormaInvoice->loan_application_no) : '',
                            'name' => $application->{$config['name_field']},
                            'mobile' => $mobile,
                            'consent_timestamp' => Carbon::now()->toDateTimeString(),
                            'lender_id' => $application->lender_id
                        ];

                        if ($ip) $data['ip'] = $ip;
                        if ($consentExists) {
                            $data['latitude'] = $consentExists->latitude;
                            $data['longitude'] = $consentExists->longitude;
                        }
                        $data['status'] = $status;
                        $view = view('admin.consumer-application.consent_logs_document', ['data' => $data]);

                        $pdf = App::make('snappy.pdf.wrapper');
                        $request = new Request(['type_of_applicant' => $applicant_type]);
                        (new ConsumerDocument($request))->uploadToS3($config['s3_folder'], $application_id_decoded, $pdf->loadHTML($view)->output());

                        $message = 'We have logged your Consent';
                        $title = 'Thank You!';
                        if($status != 'deny') {
                            $message = 'We have logged your Denial of Consent';
                            ConsumerApplicationElement::where('application_id', $application->id)
                            ->update([$config['consent_flag'] => 1]);
                        }
                    }
                } catch (\Exception $e) {
                    $cwFactory = new CloudWatchLoggerFactory();
                    $cwFactory->logger(json_encode([
                        'application_id' => $application_id,
                        'message' => $e->getMessage(),
                        'line' => $e->getLine(),
                        'trace' => $e->getTraceAsString()
                    ]));
                }
            } else {
                $message = 'We have already logged your Consent';
                $title = 'Thank You!';
            }
        }

        if (!$show_view) {
            return response()->json(['status' => 'OK', 'message' => $message], 200);
        }

        return view('front.message-redirect', ['message' => $message, 'title' => $title]);
    }


    public function submitConsent(Request $request){
        $status = $request->input('consent_status');
        $application_id = $request->input('application_id');
        $applicant_type = $request->input('applicant_type');
        $request->merge(['em' => true]);

        if ($request->has('is_api_call') && $request->is_api_call == true) {
            return $this->addConstantDocument($application_id, $applicant_type, false, $status, $request);
        } else {
            return $this->addConstantDocument($application_id, $applicant_type, true, $status, $request);
        }
    }
}