<?php

namespace App\Services;

use App\Models\Permission;
use App\Models\User;

class ClientService
{
    public function getUsers($inputData)
    {
       return (new User())->getClients($inputData);
    }
    public function getUserInfoById($id)
    {
        $result = ['status'=>false,'data'=>[]];
        if ($user = User::where(['id'=>$id])->first()) {
            $result = ['status'=>true,'data'=>$user];
        }
        return $result;

    }
    public function updateUserInfoById($inputData)
    {
        $result = null;
        if ($user = User::where('id',$inputData['id'])->first()) {
            $currentWave = $user['wave'];
            $maxWave=config('constant.MAX_WAVE');
            $currentWave = ($currentWave<$maxWave)? ($currentWave+1) : $maxWave;
            $packedData = $this->userDataPacking($inputData);
            if(!empty($inputData['reset_account'])){
                $packedData['subscription_status'] = 1;
                $packedData['wave'] = $currentWave;
                $packedData['last_payment_date'] = addDays($inputData['reset_account'],$user->last_payment_date);
            }

            $user->update($packedData);
            $result = $user;
        }
        return $result;

    }
    private function userDataPacking($inputData)
    {
        $outputData=[];

             $outputData['first_name'] = $inputData['first_name'];
             $outputData['last_name'] = $inputData['last_name'];

             $outputData['state'] = $inputData['state'];
             $outputData['phone'] = $inputData['phone'];
             $outputData['password'] = customEncrypt($inputData['password']);
            $outputData['street_no'] = $inputData['street_no'];
            $outputData['street_name'] = $inputData['street_name'];
            $outputData['city'] = $inputData['city'];
            $outputData['zip_code'] = $inputData['zip_code'];
             $outputData['credit_report_company_type'] = $inputData['credit_report_company_type'];

             $status = $inputData['status'];
              if(!empty($inputData['email']))
              {
                  $outputData['email'] = $inputData['email'];
              }
                if($inputData['status'] == 3)
                {
                    $status = 1;
                    $outputData['subscription_status'] = 2;
                    $outputData['account_cancel_datetime'] = getCurrentDate();
                }
                if(!empty($inputData['user_access_date']))
                {
                    $outputData['user_access_end_date'] = $inputData['user_access_date'];
                }
                if(!empty($inputData['birth_date']))
                {
                    $outputData['birth_date'] = $inputData['birth_date'];
                }
                if(!empty($inputData['ssn']))
                {
                    $outputData['ssn'] = $inputData['ssn'];
                }
                if(!empty($inputData['last_payment_date']))
                {
                    $outputData['last_payment_date'] = $inputData['last_payment_date'];
                }
                if(!empty($inputData['subscription_status']))
                {
                    $outputData['subscription_status'] = $inputData['subscription_status'];
                }
                if(!empty($inputData['wave'])){
                    $outputData['wave'] = $inputData['wave'];
                }
                if(!empty($inputData['last_import_date'])){
                    $outputData['last_import_date'] = $inputData['last_import_date'];
                }

        $outputData['status'] = $status;
        return $outputData;
    }

}
