<?php

namespace App\Jobs;

use App\Models\Booking;
use App\Models\Payement;
use App\Models\Transaction;
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 PayementBooking implements ShouldQueue
{
    use Dispatchable, Queueable, SerializesModels, Currencies;
    private $booking;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($booking)
    {
        $this->booking = $booking;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // if(auth()->user()->is_admin==0){
                
        //   $currencey= $account->currency_code;
        //   $account_id=$account->id;
        //   $diff_price= $this->booking->amountPaid();
        // }
        $account = auth()->user()->office->account;
        // else{
        //         $account='';
        //         $currencey='';
        //         $account_id=null;
        //         $diff_price=$this->diff_price;
        //     }
        // dd($this->booking->id,$this->booking->amountPaid(),$currencey);
      ///////////////ضريبة التغيير 
      //////////// الغرفة وسعر 
      
            DB::transaction(function() use($account){
                 if($this->booking->amountPaid() >0){

                    $mmount=$this->booking->amountPaid();
                }
                else{
                      $mmount=$this->booking->amountPaid() * -1;
                    }
             $payment=  $payment= Payement::create([
                    'booking_id' => $this->booking->id,
                    'amount' => $mmount,
                    'currency_code' => $account->currency_code
                ]);

                foreach ($this->booking->tickets as $ticket) {
                  if($ticket->amountNetPaid() >0){
 
                    $amount_ticket=  $ticket->amountNetPaid();
                   $balance= auth()->user()->office->account->balance - $ticket->amountNetPaid() ;
                      
                  }
                  else{
                     $amount_ticket=$ticket->amountNetPaid() * -1;
                     $balance=(auth()->user()->office->account->balance - $ticket->amountNetPaid()) *-1;
                  }
                  $transaction = Transaction::create([
                        'account_id' =>$account->id,
                        'ticket_id' => $ticket->id,
                        'category' => Transaction::CATEGORY_TICKET,
                        'paid_at' => now(),
                         'balance' =>$balance ,
                        'amount' => $amount_ticket,
                        'type' => Transaction::TYPE_EXPENSE,
                        'currency_code' => $account->currency_code
                    ]);
                }
    
            });
            $user_balance=auth()->user()->office->account->balance;
            if($user_balance <0){
                $user_balance *=-1;
                $user_balance->save();
                
            }
    }
}
