<?php

namespace Modules\Internal\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class CorporateServiceConfigResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {

        $data =  [
            'id' => $this->id,
            'corporate_id' => $this->corporate_id,
            'service_code' => $this->service_code,
            'status' => $this->status,
            'name' => $this->service->name,
            'description' => $this->service->description,
            'configurations' => $this->configs->pluck('value', 'name'),
            'selected_specialities' => $this->specialities->pluck('speciality.name', 'speciality_id'),
            'exclusions' => $this->specialities->map(function ($speciality) {
                return [
                    'speciality_id' => $speciality->speciality_id,
                    'rules' => $speciality->exclusions->first()->rules->map(
                        function ($rule) {
                            return [
                                'name' => $rule->name,
                                'value' => explode(',', $rule->values)
                            ];
                        }
                    ),

                ];
            }),
        ];

        $list_msc = $this->specialities->map(function ($speciality) {
            return  explode(',', $speciality->exclusions->first()->rules->where('name', 'msc')->first()->values);
        })->map(function ($item) {
            return [
                'm' => in_array('m', $item),
                's' => in_array('s', $item),
                'c' => in_array('c', $item),
            ];
        });
        // $msc = $cek->map(function ($item) {
        //     return [
        //         'name' => $item,
        //         'value' => true
        //     ];
        // });

        $data['exclusions'] = $data['exclusions']->map(function ($item, $key) use ($list_msc) {

            $item['msc'] = $list_msc[$key];
            return $item;
        });





        return $data;
    }
}
