<?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\Order;
use App\Models\Transaction;
use App\Models\Wallet;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class MappableTransactionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $reference = null;
        if($this->type === TransactionType::TOP_UP) {
            $reference = $this->owner->owner->inviters()->withPivot('invitee_reference')->first()->pivot->invitee_reference;
        }

        if($this->type === TransactionType::PAYMENT && $this->owner_type !== Wallet::class){
            $reference = $this->owner->owner->owner->reference;
        }

        return [
            'status' => 'success',
            'system' => 'SHIPPING_PORTAL',
            'type' => $this->type,
            'owner_type' => Transaction::class,
            'owner_id'=> $this->id,
            'owner_reference'=> $reference,
            'created_at'=> $this->created_at, // invoice date
        ];
    }
}
