<?php

namespace App\Console\Commands\ChannelManager;

use App\Core\Mail\LogMail;
use App\Core\Service\ChannelManagerBookingService;
use App\Exceptions\ApiErrorException;
use App\Models\Booking;
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\Log;
use Exception;

class MetaCancellationService extends Command
{

    protected $signature = 'cron:meta-cancellation-service';

    protected $description = '';
    protected $channelService;

    public function __construct(
        Mailer $mailer,
        ChannelManagerBookingService $channelManagerBookingService
    )
    {
        parent::__construct();
        $this->mailer = $mailer;
        $this->channelManagerBookingService = $channelManagerBookingService;

    }

    public function handle()
    {


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

        $today = Carbon::now()->startOfDay()->toDateString();


        if(!Carbon::parse($today)->isLastOfMonth()) {
            $this->alert(date('Y-m-d H:i:s') . ' : Not Today!');
            return false;
        }

        $fistDayOfMonth = Carbon::parse($today)->startOfMonth()->toDateString();
        $lastDayOfMonth = Carbon::parse($today)->addMonth()->startOfMonth()->toDateString();

        $cancellationBooking = Booking::where('channel_id', 1)
            ->where('status', 0)
            ->where('checkout_date', '>', $fistDayOfMonth)
            ->where('checkout_date', '<', $lastDayOfMonth)
            //->where('id', 68690)
            ->with('channelManagerBooking')
            ->with('bookingRoom')
            ->get();

        //11 - Trivago

        $cancellationBooking = $cancellationBooking ? $cancellationBooking->toArray() : [];


        foreach ($cancellationBooking as $booking) {

            try {


                if (empty($booking['channel_manager_booking'])) {
                    $this->line(date('Y-m-d H:i:s') . ' : Booking ID: ' . $booking['id']);
                    continue;
                }

                $channelBookingCheck = collect($booking['channel_manager_booking'])
                    ->where('channel_manager_id', 11)
                    ->where('type', 'Booking')
                    ->where('is_pushed', 1)
                    ->isEmpty();

                if ($channelBookingCheck) {
                    $this->line(date('Y-m-d H:i:s') . ' : Booking ID: ' . $booking['id']);
                    continue;
                }

                $channelCancelCheck = collect($booking['channel_manager_booking'])
                    ->where('channel_manager_id', 11)
                    ->where('type', 'Cancel')
                    ->where('is_pushed', 1)
                    ->isEmpty();

                if (!$channelCancelCheck) {
                    $this->line(date('Y-m-d H:i:s') . ' : Booking ID: ' . $booking['id']);
                    continue;
                }

                $channelManagerBookingCreateParam = [
                    'property_id' => $booking['property_id'],
                    'booking_id' => $booking['id'],
                    'channel_manager_id' => 11,
                    'type' => 'Cancel',
                ];

                $this->channelManagerBookingService->create($channelManagerBookingCreateParam);

                $this->info(date('Y-m-d H:i:s') . ' : Booking ID: ' . $booking['id']);


            } catch (ApiErrorException $e) {
                $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
                Log::error($message);
                $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);
            }

        }


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


    }

}
