<?php

namespace App\Console\Commands\ChannelManager;

use App\Core\Mail\LogMail;

use App\Core\Service\BookingPaymentService;
use App\Exceptions\ApiErrorException;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RemovePaymentTokenService extends Command
{
    protected $signature = 'cron:remove-payment-token-service';

    protected $description = '';
    protected $mailer;

    public function __construct(
        Mailer $mailer,
        BookingPaymentService $bookingPaymentService
    )
    {
        parent::__construct();
        $this->mailer = $mailer;
        $this->bookingPaymentService = $bookingPaymentService;
    }

    public function handle()
    {

        try {


            $this->info(date('Y-m-d H:i:s') . ' : Start');

            $bookingPaymentDataCriteria =
                [
                    'criteria' =>
                        [
                            ['field' => 'status', 'condition' => '=', 'value' => 2],
                            ['field' => 'type', 'condition' => '=', 'value' => 'ch'],
                        ],
                    'with' => ['bookingDetail.channelManager'],
                    'orderBy' => [
                        ['field' => 'id', 'value' => 'ASC']
                    ]
                ];

            $bookingPaymentData = $this->bookingPaymentService->selectPaymentData($bookingPaymentDataCriteria);
            $bookingPaymentData = $bookingPaymentData['status'] == 'success' && !empty($bookingPaymentData['data']) ? $bookingPaymentData['data'] : [];

            if (!empty($bookingPaymentData)) {
                $bookingPaymentDataCollect = collect($bookingPaymentData);
                $bookingPaymentData = $bookingPaymentDataCollect->where('booking_detail.checkout_date', '<', Carbon::now()->subDays(7))->toArray();
            }

            //$bookingPaymentData
            foreach ($bookingPaymentData as $bookingPaymentKey => $bookingPayment) {

                if (empty($bookingPayment['booking_detail']['channel_manager_id'])) {
                    continue;
                }

                $channelManagerId = $bookingPayment['booking_detail']['channel_manager_id'];

                switch ($channelManagerId) {
                    case 2 :

                        $channelService = App::make("App\Core\Service\ChannelManager\\{$bookingPayment['booking_detail']['channel_manager']['name']}");

                        $removeCreditCardToken = $channelService->removeCreditCardToken($bookingPayment['data']);

                        if (!$removeCreditCardToken['status']) {

                            //Hata maili atılsın
                            $this->error(date('Y-m-d H:i:s') . ' : Channel: ' . $bookingPayment['booking_detail']['channel_manager']['name'] . ' Token: ' . $bookingPayment['data']);

                        } else {

                            $this->bookingPaymentService->updatePaymentData($bookingPayment['id'], ['status' => 1]);

                            $this->info(date('Y-m-d H:i:s') . ' : Channel: ' . $bookingPayment['booking_detail']['channel_manager']['name'] . ' Token: ' . $bookingPayment['data']);

                        }

                        break;

                    default;
                        break;

                }


            }


            $this->info(date('Y-m-d H:i:s') . ' : Finished');

        } catch (ApiErrorException $e) {
            $this->error(date('Y-m-d H:i:s') . ' : Error: ' . $e->getMessage());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $this->error(date('Y-m-d H:i:s') . ' : Error: ' . $message);
        }

    }

}
