<?php

namespace App\Jobs;

use App\Models\Refund;
use App\Models\Ticket;
use App\Models\Service;
use App\Models\Transaction;
use App\Models\SegmentService;
use App\Traits\Currencies;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class Refund1Booking implements ShouldQueue
{
    use Dispatchable, Queueable, SerializesModels, Currencies;
    private $ticket;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($ticket)
    {
        $this->ticket = $ticket;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
       $amount_refund=Transaction::where('ticket_id',  $this->ticket->id)->where('amount','!=',0)->first();
        $account = auth()->user()->office->account;
        $date = $this->ticket->created_at;

      
            $this->ticket->ticket_status = Ticket::STATE_‫‪REFUNDED‬‬;
   
          
          if($this->ticket->hasRetour()){
              
                 $office_commission=auth()->user()->office->booking_commission /100;
                 $price=$this->ticket->segment->roomType->service->price;
                 $price_service = $price -$price * $office_commission  ;
                 $price2=$this->ticket->price;
                 $price_tikect=$price2 - $price2 *$office_commission;
              
               if($this->ticket->car()->exists()){
                 $car=$this->ticket->car->car->service;
                 $price_car1=$car->price -$car->price * $office_commission;
                 $price_car2=$car->price_two -$car->price_two * $office_commission;
                 $price_ticket=SegmentService::where('ticket_id',$this->ticket->id)->first();
                 $price_commission=$price_ticket->price -$price_ticket->price * $office_commission  ;
                 
                 
                   $amount1= $price_commission- ($price_car1-$price_car2);
                   $amount2=$price_service-$price_tikect;
                   $amout_transaction1=$amount2; 
                   $amout_transaction= $amount1 +$amout_transaction1;
                   $amount=$amout_transaction;

               }
               else{
              
                   $amount=$price_service-$price_tikect;
                   $amout_transaction=$amount;
              }
    
          
     
           
       }

       
        DB::transaction(function () use ($account,$amount,$amout_transaction,$amount_refund) {
            $this->ticket->save();

            Refund::create([
                'ticket_id' => $this->ticket->id,
                'amount' => $amount,
                'currency_code' => $account->currency_code
            ]);

            Transaction::create([
                'account_id' => $account->id,
                'ticket_id' => $this->ticket->id,
                'category' => Transaction::CATEGORY_TICKET,
                'paid_at' => now(),
                'balance' =>(auth()->user()->office->account->balance - $amout_transaction ) +($amount_refund->amount *-1) ,

                'amount' => $amout_transaction,
                'type' => Transaction::TYPE_INCOME,
                'currency_code' => $account->currency_code
            ]);

            if($this->ticket->segment->tickets->count()<1){
                $this->ticket->segment->segment_status= $this->ticket->ticket_status;
                $this->ticket->segment->save();
            }
            if($this->ticket->segment->booking->tickets->count()<1){
                $this->ticket->segment->booking->status= $this->ticket->ticket_status;
                $this->ticket->segment->booking->save();
            }

        });
        
      
        return $this->ticket;
    }


}
