<?php

namespace App\Http\Resources;

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

class PaymentVoucherResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $supplier = $this->relationLoaded('supplier') ? $this->whenLoaded('supplier') : null;
        $wayToPay = $this->relationLoaded('wayToPay') ? $this->whenLoaded('wayToPay') : null;

        return [
            'id' => $this->id,
            'idsupplier' => $this->idsupplier,
            $this->mergeWhen($supplier, fn() => [
                'supplier' => $supplier->name_rz,
                'rut' => $supplier->rut,
            ]),
            'idway_to_pay' => $this->idway_to_pay,
            $this->mergeWhen($wayToPay, fn() => [
                'way_to_pay' => $wayToPay->name
            ]),
            'idsource_bank' => $this->idsource_bank,
            'idreceiving_bank' => $this->idreceiving_bank,
            'series' => $this->series,
            'entry_date' => $this->entry_date,
            'document_date' => $this->document_date,
            'amount' => $this->amount,
            'idcompany' => $this->idcompany,
            'status' => $this->status,
            'created_at' => $this->created_at ? Carbon::parse($this->created_at)->format('Y-m-d H:i:s') : ''
        ];
    }
}
