<?php

namespace App\Http\Resources;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Models\Transaction;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class GroupResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        // dd($this->groupTransactions);
        $payment_transaction = Transaction::where('payment_reference', $this->reference)->first();

        return [
            'id' => $this->id,
            'original_amount' => (float) $this->original_amount,
            'original_currency' => new CurrencyResource($this->original_currency),
            'issuer_name' => $this->issuerCompany->name,
            'issuer_id' => $this->issuerCompany->id,
            'amount' => (float) $this->amount,
            'service_charge' => (float) $this->amount,
            'currency' => new CurrencyResource($this->currency),
            'created_at' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A'),
            'currency_rate' => (float) $this->currency_rate,
            'status' => $this->status,
            'status_name' => ApprovalStatus::APPROVAL_STATUS_ID[$this->status],
            'payment_method' => $this->payment_method,
            'payment_method_name' => ucwords(PaymentMethodType::PAYMENT_METHODS_ID[$this->payment_method]),
            'invoices' => GroupTransactionResource::collection($this->groupTransactions),
            'reference' => $this->reference,
            'payment_transaction' => $payment_transaction ? [
                'id' => $payment_transaction->id,
                'status' => $payment_transaction->status,
                'status_name' => ApprovalStatus::APPROVAL_STATUS_ID[$payment_transaction->status],
                'documents' => $this->status === ApprovalStatus::APPROVED ? DocumentResource::collection($payment_transaction->documents->where('status', ApprovalStatus::APPROVED)) : DocumentResource::collection($payment_transaction->documents->where('status', ApprovalStatus::PENDING_VERIFICATION)),
            ] : []
        ];
    }
}
