<?php

namespace App\Http\Resources;

use App\Classes\General\Eloquent\Filters\TransactionServiceId;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Transaction;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Log;

class WalletTransactionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $description = '';
        $current_running_balance = $request['running_balance'];
        switch((int) $this->type){
            case TransactionType::TOP_UP:
                $description = (double) $this->amount.' Credit Top up';
                $request['running_balance'] = bcsub($request['running_balance'], $this->amount, 5);
                break;
            case TransactionType::CREDIT_NOTE:
                $description = 'Credit Voucher for '.$this->payment_reference;
                $request['running_balance'] = bcsub($request['running_balance'], $this->amount, 5);
                break;
            case TransactionType::PAYMENT:
                $invoice = Transaction::where('payment_reference', $this->bill_no)->first();
                if(!$invoice) {
                    Log::channel('paymentUnknownOrderLog')->info('ID: ' . $this->id);
                    $description = 'Payment for unknown invoice, please contact tech support.';
                    break;
                }

                $order = $invoice->owner->owner->owner;
                if(!$order) {
                    Log::channel('paymentUnknownOrderLog')->info('ID: ' . $this->id);
                    $description = 'Payment for unknown order, please contact tech support.';
                    break;
                }

                $request['running_balance'] = bcadd($request['running_balance'], $this->amount, 5);
                $marking = $order->reference;
                $description = 'Payment For order refs.'.'<a href="'.route('order.details', $marking).'">'.$marking.'</a>';
                break;
            case 11:
                $request['running_balance'] = bcadd($request['running_balance'], $this->amount, 5);
                $description = 'Debit Voucher for '.$this->payment_reference;
                break;
            case 15:
                $request['running_balance'] = bcsub($request['running_balance'], $this->amount, 5);
                $description = (double) $this->amount.' Credit Top up';
                break;

        }

        return [
            'id' => (int) $this->id,
            'type' => (int) $this->type,
            'marking' => $this->owner->owner->reference,
            'bill_no' => $this->bill_no,
            'reference' => $this->payment_reference,
            'payment_method' => (float) $this->payment_method,
            // 'issuer_name' => $this->issuerCompany->name,
            'amount' => (double) $this->amount,
            'running_balance' => (double) $current_running_balance,
            'service_charge' => (double) $this->service_charge,
            'tax' => (double) $this->tax,
            'status' => (int) $this->status,
            'description' => $description,
            'details' => TransactionDetailResource::collection($this->transactionDetails),
            'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y h:i:s A'),
            'created_at' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A')
        ];
    }
}
