<?php

namespace App\Http\Resources;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Group;
use App\Models\Order;
use App\Models\Transaction;
use App\Models\Wallet;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class TransactionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $order = null;
        $groupTransactions = null;

        if ($this->owner instanceof Transaction) {
            if ($this->owner) {
                if ($this->owner->owner) {
                    $order = new OrderResource($this->owner->owner->owner);
                }
            }
        } else if (!($this->owner instanceof Transaction) && !($this->owner instanceof Wallet)) {
            if ($this->owner) {
                $order = new OrderResource($this->owner->owner);
            }
        } else {
            $group = Group::where('reference', $this->payment_reference)->first();
            if ($group) {
                $groupTransactions = GroupTransactionResource::collection($group->groupTransactions);
            }

        }

        return [
            'id' => $this->id,
            'owner_type' => $this->owner_type,
            'order' => $order,
            'group_transactions' => $groupTransactions,
            'group_reference' => $groupTransactions ? ($group ? $group->reference : null )  : null,
            'documents' => $groupTransactions ? DocumentResource::collection($this->documents->where('status', ApprovalStatus::PENDING_VERIFICATION)) : DocumentResource::collection($this->documents),
            'type' => (int) $this->type,
            'bill_no' => $this->bill_no,
            'amount' => (double) $this->amount,
            'payment_method' => (int) $this->payment_method,
            'payment_reference' => $this->payment_reference,
            'outstanding' => (double) $this->amount - ($this->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('amount')),
            'floating' => (double) $this->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION])->sum('amount'),
            'service_charge' => (double) $this->service_charge,
            'tax' => (double) $this->tax,
            'original_amount' => (double) $this->original_amount,
            'currency' => new CurrencyResource($this->currency),
            'original_currency' => new CurrencyResource($this->original_currency),
            'currency_rate' => (double) $this->currency_rate,
            'status' => (int) $this->status,
            'details' => TransactionDetailResource::collection($this->transactionDetails),
            'transactions' => TransactionResource::collection($this->transactions),
            'payment_attempts' => TransactionResource::collection(
                $this->transactions()
                    ->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION)
                    ->get()
            ),
            'payment_history' => TransactionResource::collection(
                $this->transactions()
                    ->payments()
                    ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::COMPLETED, ApprovalStatus::REJECTED])
                    ->get()
            ),
            'remarks' => RemarkResource::collection($this->remarks),
            'expires_on' => Carbon::parse($this->expires_on)->format('d-m-Y h:s:i'),
            'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y'),
            'created_at' => Carbon::parse($this->created_at)->format('d-m-Y')
        ];
    }
}
