<?php

namespace App\Console\Commands\Jobs;

use App\Core\Service\BookingService;
use App\Exceptions\ApiErrorException;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Exception;

class BookingCommissionService extends Command
{
    protected $signature = 'cron:commission-service';

    protected $description = '';

    private $bookingService;

    public function __construct(
        BookingService $bookingService
    )
    {
        parent::__construct();
        $this->bookingService = $bookingService;
    }

    public function handle()
    {

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

        try {


            $bookingListParam = [
                'criteria' => [
                    //['field' => 'id', 'condition' => '=', 'value' => 600],
                    //['field' => 'property_id', 'condition' => '=', 'value' => 313],
                    ['field' => 'commission', 'condition' => '=', 'value' => null],
                    ['field' => 'commission_rate', 'condition' => '=', 'value' => null]
                ],
                'with' => ['bookingChannel.propertyChannelCategory', 'bookingProperty'],
                "take" => 10000,
                "orderBy" => [
                    ["field" => "id", "value" => "ASC"]
                ],
            ];

            $bookingList = $this->bookingService->select($bookingListParam);

            if ($bookingList['status'] != 'success' || ($bookingList['status'] == 'success' && empty($bookingList['data']))) {
                throw new ApiErrorException('Property list not found!');
            }

            foreach ($bookingList['data'] as $booking) {


                $commission = null;
                $commissionRate = null;
                //$booking['booking_property']['commission'] = 15;//TOOD: DEL
                switch ($booking['booking_channel']['channel_category_id']) {
                    case "3" :

                        if (fillOnUndefined($booking['booking_property'], 'commission') && $booking['booking_property']['commission'] > 0) {
                            $commissionRate = $booking['booking_property']['commission'];
                            $commission = moneyDoubleFormatDecimal($booking['booking_property']['commission'] * $booking['total'] / 100);
                        }

                        break;
                    case "2" :

                        if (fillOnUndefined($booking['booking_property'], 'commission_offline') && $booking['booking_property']['commission_offline'] > 0) {
                            $commissionRate = $booking['booking_property']['commission_offline'];
                            $commission = moneyDoubleFormatDecimal($booking['booking_property']['commission_offline'] * $booking['total'] / 100);
                        }

                        break;
                    case "4" :

                        if (fillOnUndefined($booking['booking_property'], 'commission_channel') && $booking['booking_property']['commission_channel'] > 0) {
                            $commissionRate = $booking['booking_property']['commission_channel'];
                            $commission = moneyDoubleFormatDecimal($booking['booking_property']['commission_channel'] * $booking['total'] / 100);
                        }

                        break;
                    case "7" :

                        if (fillOnUndefined($booking['booking_property'], 'commission_wholesaler') && $booking['booking_property']['commission_wholesaler'] > 0) {
                            $commissionRate = $booking['booking_property']['commission_wholesaler'];
                            $commission = moneyDoubleFormatDecimal($booking['booking_property']['commission_wholesaler'] * $booking['total'] / 100);
                        }

                        break;
                }

                if (!empty($commission) && !empty($commissionRate)) {
                    $this->bookingService->update($booking['id'], ['commission' => $commission, 'commission_rate' => $commissionRate]);
                    $this->info(date('Y-m-d H:i:s') . ' Property: ' . $booking['booking_property']['name'] . ' Code: ' . $booking['booking_code'] . ' Commission: ' . $commission . ' Commission Rate: ' . $commissionRate);
                } else {

                    $this->bookingService->update($booking['id'], ['commission' => 0, 'commission_rate' => 0]);
                    $this->error(date('Y-m-d H:i:s') . ' Property: ' . $booking['booking_property']['name'] . ' Code: ' . $booking['booking_code'] . ' Commission: ' . $commission . ' Commission Rate: ' . $commissionRate);
                }

                //sleep(1);

            }


        } catch (ApiErrorException | Exception $e) {
            $this->error(date('Y-m-d H:i:s') . ' ERROR:' . $e->getMessage());
        }


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


    }
}
