<?php

namespace Modules\Internal\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class DiagnosisExclusionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $data = [
            'id' => $this->id,
            'code' => $this->exclusionable->code,
            'name' => $this->exclusionable->name,
            'diagnosis_type' => $this->exclusionable->type,
            'service_code' => $this->service_code,
            'type' => $this->type,
            'active' => $this->active,
            'rules' => $this->rules->mapToGroups(function ($item, $key) {
                return [$item['name'] => $item['values']];
            })
        ];

        $msc = explode(',', $this->rules->where('name', 'msc')->first()->values ?? '');
        $list_msc = [
            'm' => in_array('m', $msc),
            's' => in_array('s', $msc),
            'c' => in_array('c', $msc),
        ];

        $gender = explode(',', $this->rules->where('name', 'gender')->first()->values ?? '');
        $list_gender = [
            'male' => in_array('male', $gender),
            'female' => in_array('female', $gender),
        ];

        $min_age = $this->rules->where('name', 'min_age')->first()->values ?? '';
        $max_age = $this->rules->where('name', 'max_age')->first()->values ?? '';
        $plan = $this->rules->where('name', 'plan')->first()->values ?? '';

        $value_plan = [
            'plan' => $plan,
            'gender' => $list_gender,
            'msc' => $list_msc,
            'min_age' => $min_age,
            'max_age' => $max_age,
        ];

        $data['value_rules'] = $value_plan;



        return $data;
    }
}
