<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\CategoryDescription;
use App\PageDescription;
use App\Models;
use App\User;
use App\Http\Resources\CountryResource;
use App\Http\Resources\AttributeResource;
use App\Http\Resources\CategoryResource;
use App\Http\Resources\TypeResource;
use App\Http\Resources\MemberResource;
use App\Http\Resources\ConsultResource;

class ChannelResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
     
    public function toArray($request)
    {
        
      
       
        $data =  [
            'id' => $this->id,
            'channel'=>$this->channel,
            'name' => $this->name,
            'description' => $this->description,
            'type_user'=>$this->type,
            'status'=>$this->status,
            'date'=>date('F j, Y',strtotime($this->created_at)),
            'rate'=>rating($this->id),
            'category'=>new CategoryResource($this->whenLoaded('category')),
            'type'=>new TypeResource($this->whenLoaded('typing')),
            
            
            
            



      ];
      if(\Auth::user()){
          $data_merged = [
                   'isReport'=>reported($this->id,\Auth::user()->id),
                    
                     
                      ];
              $data= array_merge($data, $data_merged);
      }
      else{
          $data_merged = [
                   'isReport'=>false,
                    
                     
                      ];
              $data= array_merge($data, $data_merged);
      }
      if(\Auth::user()){
           if(\Auth::user()->role == 'member'){
          
           $data_merged = [
                   'isRated'=>rated($this->id,\Auth::user()->id),
                    
                     
                      ];
              $data= array_merge($data, $data_merged);
                if(!is_null($this->target_id)){
          $consult = User::with(['experience','country','category','certification'])->find($this->target_id);
          $data_merged = [
                      'consult' => new ConsultResource( $consult ),
                      'member'=> new ConsultResource( $consult ),
                     
                      ];
              $data= array_merge($data, $data_merged);
      }
      else{
         
        
        $data_merged = [
            'member'=>null,
                      'consult'=>null,
                     
                      ];
              $data= array_merge($data, $data_merged);
    
      }
           }
            else{
                
                   $data_merged = [
                       'isRated'=>ratedconsult($this->id),
                  'member'=>new MemberResource($this->whenLoaded('member')),
            'consult'=>new ConsultResource($this->whenLoaded('consult')),
                     
                      ];
              $data= array_merge($data, $data_merged);
                
            
          
      }
      }
     
    
      return $data;
    }
}
