<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Enums\Status;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PaymentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $gatewayUrl = $this->ref_url ?: $this->url;

        return [
            'id' => $this->id,
            'transaction_id' => $this->transaction_id,
            'ref_id' => $this->ref_id,
            'ref_url' => $this->ref_url,
            'price' => (int) $this->price,
            'gateway' => $this->gateway,
            'status' => Status::tryFrom((int) $this->status)?->label() ?? (string) $this->status,
            'project_id' => $this->project_id,
            'card_number' => $this->card_number,
            'cellphone' => $this->cellphone,
            'email' => $this->email,
            'payment_url' => $gatewayUrl,
            'gateway_url' => $gatewayUrl,
            'created_at' => $this->created_at?->toISOString(),
            'created_by' => new UserResource($this->whenLoaded('createdBy')),
            'project' => new ProjectResource($this->whenLoaded('project')),
        ];
    }
}
