<?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->corporateServiceSpecialities->where('active', true)->pluck('speciality.name', 'speciality_id'),
            'exclusions' => $this->corporateServiceSpecialities->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->corporateServiceSpecialities->map(function ($speciality) {
            $exclusions = $speciality->exclusions->first();
            
            if ($exclusions) {
                $rules = $exclusions->rules->where('name', 'msc')->first();
        
                if ($rules) {
                    $values = $rules->values ?? '';
                    $item = explode(',', $values);
                } else {
                    // Handle case where 'rules' with name 'msc' is not found
                    $item = [];
                }
            } else {
                // Handle case where 'exclusions' is not found
                $item = [];
            }
        
            return [
                'm' => in_array('m', $item),
                's' => in_array('s', $item),
                'c' => in_array('c', $item),
            ];
        });
        
        $list_gender = $this->corporateServiceSpecialities->map(function ($speciality) {
            $exclusions = $speciality->exclusions->first();
            
            if ($exclusions) {
                $rules = $exclusions->rules->where('name', 'gender')->first();
        
                if ($rules) {
                    $values = $rules->values ?? '';
                    $item = explode(',', $values);
                } else {
                    // Handle case where 'rules' with name 'gender' is not found
                    $item = [];
                }
            } else {
                // Handle case where 'exclusions' is not found
                $item = [];
            }
        
            return [
                'male' => in_array('male', $item),
                'female' => in_array('female', $item),
            ];
        });
        

        $min_age = $this->corporateServiceSpecialities->map(function ($speciality) {
            $exclusions = $speciality->exclusions->first();
            
            if ($exclusions) {
                $rules = $exclusions->rules->where('name', 'min_age')->first();
        
                if ($rules) {
                    return $rules->values ?? '';
                }
            }
        
            return '';
        });
        
        $max_age = $this->corporateServiceSpecialities->map(function ($speciality) {
            $exclusions = $speciality->exclusions->first();
            
            if ($exclusions) {
                $rules = $exclusions->rules->where('name', 'max_age')->first();
        
                if ($rules) {
                    return $rules->values ?? '';
                }
            }
        
            return '';
        });
        
        $plan = $this->corporateServiceSpecialities->map(function ($speciality) {
            $exclusions = $speciality->exclusions->first();
            
            if ($exclusions) {
                $rules = $exclusions->rules->where('name', 'plan')->first();
        
                if ($rules) {
                    return $rules->values ?? null;
                }
            }
        
            return null;
        });
        
        $data['exclusions'] = $data['exclusions']->map(function ($item, $key) use (
            $list_msc,
            $list_gender,
            $min_age,
            $max_age,
            $plan,
        ) {
            $item['msc'] = $list_msc[$key];
            $item['gender'] = $list_gender[$key];
            $item['min_age'] = $min_age[$key];
            $item['max_age'] = $max_age[$key];
            $item['plan'] = $plan[$key];
            return $item;
        });





        return $data;
    }
}
