<?php

namespace App\Http\Resources\V2;

use App\Classes\ValueObjects\Constants\TransactionType;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\V2\BookingV2Resource;
use App\Http\Resources as V1;

class ListTransactionJobResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $booking = null;
        $bank = null;
        //Check if Transaction of type PAYMENT has an override for recipient bank - starts
        if(in_array((int)$this->type, [TransactionType::BILL, TransactionType::REFUND])){
            $booking = $this->owner->owner;
            $bank = $this->owner->bank ?? $booking->bank;
        }
        else{
            $booking = $this->owner;
            $bank = $this->bank ?? $booking->bank;
        }
        //Check if Transaction of type PAYMENT has an override for recipient bank - ends
        $days = $this->created_at->endOfDay()->addWeekdays($booking->service_id === 3 ? 3 : 1);

        $transactionBillQuery = $this->when((int) $this->type === TransactionType::PAYMENT,  $this->transactions()->bills()->first());
        $transactionRefundsQuery = $this->when((int) $this->type === TransactionType::PAYMENT,  $this->transactions()->refunds()->get());

        if($this->userInfo){
            $booking->userInfo = $this->userInfo;
            if ($transactionBillQuery) {
                $transactionBillQuery->userInfo = $this->userInfo;
            }
            if ($transactionRefundsQuery) {
                foreach ($transactionRefundsQuery as $item) {
                    $item['userInfo'] = $this->userInfo;
                }
            }
        }
        $transactionBill = new TransactionV2Resource($transactionBillQuery);
        $transactionRefunds = TransactionV2Resource::collection($transactionRefundsQuery);

        return [
            'id' => $this->id,
            'booking' => new BookingV2Resource($booking),
            'type' => (int) $this->type,
            'bill_no' => $this->bill_no,
            'payment_reference' => $this->payment_reference,
            'payment_method' => (float) $this->payment_method,
            'recipient_bank_account' => new V1\BankResource($bank),
            'issuer_name' => $this->issuerCompany->name,
            'issuer_id' => $this->issuerCompany->id,
            'amount' => (double) $this->amount,
            'original_amount' => (double) $this->original_amount,
            'currency' => new V1\CurrencyResource($this->currency),
            'original_currency' => new V1\CurrencyResource($this->original_currency),
            'service_charge' => (double) $this->service_charge,
            'tax' => (double) $this->tax,
            'currency_rate' => (double) $this->currency_rate,
            'status' => (int) $this->status,
            'details' => V1\TransactionDetailResource::collection($this->transactionDetails),
            'documents' => new DocumentV2Resource($this->documents()->first()),
            'transaction_bill' => $transactionBill,
            'transaction_refunds' => $transactionRefunds,
            'expires_on' => Carbon::parse($this->expires_on)->format('d-m-Y h:i:s A'),
            'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y h:i:s A'),
            'interval' => [
                'value' => $days->gt(Carbon::now()) ? '+' : '-',
                'duration' => $days->diff(Carbon::now())->format('%d'),
            ],
            'redemption' => new V1\VoucherRedemptionResource($this->voucherRedemption)
        ];
    }
}
