<?php

namespace Modules\Client\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class ClaimShowResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $data = parent::toArray($request);
        // $data['']
        $data['benefit_items'] = $this->items
            ->filter(function ($item) {
                return $item->claim_itemable_type == Benefit::class;
            })
            ->map(function ($item) {
                $itemData = $item->claim_itemable->toArray();
                $itemData['nominal_dicover'] = $item['nominal_dicover'] ?? 0;
                $itemData['nominal_ditagihkan'] = $item['nominal_ditagihkan'] ?? 0;
                $itemData['nominal_total'] = $item['nominal_total'] ?? 0;

                // For React Frotnend
                $itemData['biaya_diajukan'] = $itemData['nominal_ditagihkan'];
                $itemData['biaya_disetujui'] = $itemData['nominal_dicover'];

                return $itemData;
            });

        $data['primary_diagnosis'] = $this->diagnoses->filter(function ($diagnosis) {
            return $diagnosis->type == 'primary';
        })->values();
        $data['secondary_diagnosis'] = $this->diagnoses->filter(function ($diagnosis) {
            return $diagnosis->type == 'secondary';
        })->values();

        return $data;
    }
}
