<?php

namespace App\Classes\Jobs\Commands\V2;


use App\Classes\Modules\Jobs\DataTransferObjects\DeleteOrderV2CommandObject;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Booking;


class DeleteOrderV2CommandJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

      /** @var DeleteOrderV2CommandObject */
      private $deleteOrderV2CommandObject;

      /**
       * DeleteOrderV2CommandJob constructor.
       * @param DeleteOrderV2CommandObject $deleteOrderV2CommandObject
       */
      public function __construct(DeleteOrderV2CommandObject $deleteOrderV2CommandObject)
      {
          $this->deleteOrderV2CommandObject = $deleteOrderV2CommandObject;
      }


    public function handle()
    {
        Log::info(Carbon::now() . ': Start job - Change refunded booking status to expired.');
        $start   = new Carbon();

        $bookings_reference = $this->deleteOrderV2CommandObject->getBookingsReference();
        // $bookings_reference = '28546,38599,44487,71086,70133,58580,42831,96028,41188,33894,95877,86732,31894,50962,44215,92894,40968,30303,89762,74693,45271,27169';
        $bookings_reference = explode(',', $bookings_reference);


        foreach ($bookings_reference as $reference) {
            $booking = Booking::where('marking', $reference)->first();

            if (!$booking) {
                $this->logOutput('Booking not found: ' . $reference);
            } else {
                $payment = $booking->transactions()
                    ->payments()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
                    ->first();

                $payment->status = ApprovalStatus::EXPIRED;
                $payment->save();

                $this->logOutput('Booking ' . $reference . ' Payment deleted: ' . $payment->id);
            }
        }



        $end = new Carbon();
        $elapsedTime = $start->diff($end)->format('%H:%I:%S');
        Log::info(Carbon::now() . ': End job - Change refunded booking status to expired. ElapsedTime: ' . $elapsedTime . '.');
        $this->logOutput('Process ended. ElapsedTime: ' . $elapsedTime);
    }

    public function logOutput($text) //cief todo: what is the purpose of this?
    {
        if (is_array($text)) {
            $text = implode(', ', $text);
        }

        Log::info(Carbon::now() . ' [DeleteOrderV2] : ' . $text);
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){

        }
        else{
            $filePath = storage_path('logs/delete-orders.log');
            $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' ' . $text . PHP_EOL;
            file_put_contents($filePath, $textToAppend, FILE_APPEND);
        }
    }
}
