<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class WithdrawalRequestResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'bank_card_id' => $this->bank_card_id,
            'amount' => (int) $this->amount,
            'shaba' => $this->shaba,
            'account_holder_name' => $this->account_holder_name,
            'wallet_balance_at_request' => $this->wallet_balance_at_request !== null ? (int) $this->wallet_balance_at_request : null,
            'transfer_ref' => $this->transfer_ref,
            'status' => $this->status,
            'admin_note' => $this->when(
                $request->user()?->hasRole('admin') || $request->user()?->id === $this->user_id,
                $this->admin_note
            ),
            'processed_at' => $this->processed_at?->toISOString(),
            'created_at' => $this->created_at?->toISOString(),
            'updated_at' => $this->updated_at?->toISOString(),
            'bank_card' => new BankCardResource($this->whenLoaded('bankCard')),
            'user' => new UserResource($this->whenLoaded('user')),
        ];
    }
}
