<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class ContentLogger extends Model
{
    protected $table = 'application_data_logs';

    public function user(){
        return $this->hasOne('App\User','id','user_id');
    }

    public function logger($component, $model, $is_new = false){

        $id = $component->id ?? 0;
        if ($model == ConsumerApplication::$model) {
            $beforeData = [
                'lender_id' => $component->lender_id ?? '',
                'monthly_income' => $component->monthly_income ?? '',
                'name' => $component->name ?? '',
                'loan_amount' => $component->loan_amount ?? '',
                'tenure' => $component->tenure ?? '',
                'dob' => $component->dob ?? '',
                'pan_card' => $component->pan_card ?? '',
                'current_address_line_1' => $component->current_address_line_1 ?? '',
                'current_address_pincode' => $component->current_address_pincode ?? '',
                'current_address_city' => $component->current_address_city ?? '',
                'current_address_state' => $component->current_address_state ?? '',
                'current_address_landmark' => $component->current_address_landmark ?? '',
                'current_address_status' => $component->current_address_status ?? '',
                'permanent_address_line_1' => $component->permanent_address_line_1 ?? '',
                'permanent_address_pincode' => $component->permanent_address_pincode ?? '',
                'permanent_address_city' => $component->permanent_address_city ?? '',
                'permanent_address_state' => $component->permanent_address_state ?? '',
                'permanent_address_landmark' => $component->permanent_address_landmark ?? '',
                'permanent_address_status' => $component->permanent_address_status ?? '',
                'email' => $component->email ?? '',
                'mobile_number' => $component->mobile_number ?? '',
                'employee_status' => $component->employee_status ?? '',
                'merchant_id' => $component->merchant_id ?? '',
                'offer_id' => $component->offer_id ?? '',
                'approved_amount' => $component->approved_amount ?? '',
                'power_savings' => $component->application_element->inverter_power_savings ?? '',
                'co_applicant_name' => $component->co_applicant_name ?? '',
                'co_applicant_email' => $component->co_applicant_email ?? '',
                'co_applicant_mobile_number' => $component->co_applicant_mobile_number ?? '',
                'co_applicant_address_line_1' => $component->co_applicant_address_line_1 ?? '',
                'co_applicant_address_pincode' => $component->co_applicant_address_pincode ?? '',
                'co_applicant_address_city' => $component->co_applicant_address_city ?? '',
                'co_applicant_address_state' => $component->co_applicant_address_state ?? '',
                'co_applicant_address_landmark' => $component->co_applicant_address_landmark ?? '',
                'co_applicant_address_status' => $component->co_applicant_address_status ?? '',
                'co_applicant_pan_card' => $component->co_applicant_pan_card ?? '',
                'co_applicant_monthly_income' => $component->co_applicant_monthly_income ?? '',
                'co_applicant_employee_status' => $component->co_applicant_employee_status ?? '',
                'is_co_applicant' => $component->co_applicant_name ? 1 : 0];
        }else if ($model == Bank::$model) {
                $id = $component->application_id;
                $beforeData = [
                    'name' => $component->name ?? '',
                    'account_type' => $component->account_type ?? '',
                    'bank_name' => $component->bank_name ?? '',
                    'account_number' => $component->account_number ?? '',
                    'branch_name' => $component->branch_name ?? '',
                    'ifsc_code' => $component->ifsc_code ?? '',
                    'karza_response' => $component->karza_response ?? ''
                ];
        }


        $user_id = Auth::user() ? Auth::user()->id : (request()->has('user_id') ? request()->get('user_id') : 107);

        $date = Carbon::now()->toDateTimeString();
        $afterDataItems = [];
        if (isset($beforeData) && $beforeData) {
            foreach ($beforeData as $key => $item) {
                if (request()->has($key) && ($is_new || (trim(request()->get($key)) != trim($item)))) {
                    $new = trim(request()->get($key));
                    if ($key == 'is_co_applicant') {
                        $new = $item;
                        if ($item && !trim(request()->get('co_applicant_name'))) {
                            $new = 0;
                        } else if (!$item && trim(request()->get('co_applicant_name'))) {
                            $new = 1;
                        }

                        if ($item == $new) {
                            continue;
                        }
                    }
                    if(($is_new && $new) || (trim($item) != trim($new))) {
                        if($is_new){
                            $item = '';
                        }
                        $afterDataItems[$key] = ['old' => trim($item), 'new' => $new];
                    }
                } else {
                    unset($beforeData[$key]);
                }
            }

            if ($afterDataItems) {
                $this->insert(['application_id' => $id, 'data' => json_encode($afterDataItems), 'status' => $component->state ?? '', 'user_id' => $user_id, 'created_at' => $date, 'model' => $model]);
            }
        }
    }
}
