<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\GateConfiguration;
use App\Models\Settings;
use App\Models\Transaction;
use App\Models\FastagEntry;
use App\Models\FastagErrorList;
use DB;
use App\Traits\FastagEngineCallAPI;

class FastagPaymentStatus extends Command
{
    use FastagEngineCallAPI;

    public $Gate;
    public $Settings;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'FastagPaymentStatus';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Fastag Payment Status Check';

    /**
     * Execute the console command.
     *
     * @return mixed
     */

    public function handle()
    {
        DB::beginTransaction();
        $logPath = storage_path() . '/logs/fastag_req_pay/';
        $fastagEntries =  DB::select("SELECT *  from `sps_transactions` where  (fastag_pay is null OR  fastag_pay = 'inprocess') AND exit_type = 'FASTag' AND deleted_at is null");
        $vehicleDetails = [];
        $this->Gate = new GateConfiguration();
        $this->Settings = new Settings();
        $holeTime = 0; //1 min
        try {
            $now = strtotime(date('Y-m-d H:i:s'));
            if (!empty($fastagEntries)) {

                foreach ($fastagEntries as $entry) {

                        try {

                            $entry = (array)$entry;
                            $response = $this->fastagPaymentStatus($entry);
                            //$response = $this->fastagReqChkTxn($entry);

                            //print_r($response);
                            //die("here");
                            if($response != false){

                                if(!empty($response['response_message'])){

                                    $data["fastag_pay"] = $response['response_message'];
                                    $responseCode = $response['response_code'];
                                    $transactionId = $entry['id'];

                                    $errorMsg = FastagErrorList::where('error_code', $responseCode)->get()->first();

                                    if (!empty($errorMsg)) {
                                        $data["fastag_comment"] = $responseCode."|".$errorMsg->description;
                                    }

                                    // $data["fastag_messageId"] = $response;
                                    // $data["fastag_pay"] = "inprocess";
                                    // $data["fastag_comment"] = "ReqPay Api intiated, Waiting for response";
                                    $updateTransaction = Transaction::where('id', $transactionId)
                                    ->update($data);
                                    DB::commit();
                                    // $this->createLog(true, $logPath, "FASTag Request Pay", $entry, json_encode($response));
                                }
                            }


                        } catch (\Exception $exception) {
                            print_r($exception->getMessage());
                            //DB::rollback();
                            throw new \Exception("FASTag Req Tag Details API ");
                        }
                    //}
                }
            } else {
                throw new \Exception("FASTag Req Tag Details API -- Data not found");
            }

        } catch (\Exception $exception) {
            DB::rollback();
            $this->createLog(false, $logPath, "FASTag Request Pay", json_encode($fastagEntries), $exception->getMessage());
        }
    }
}
