<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Language;
use App\User;
use App\Setting;
use App\ProductDescription;
use App\Product;
use App\Certification;
use App\Experience;
use App\CertificationDescription;
use App\ExperienceDescription;
use App\Page;
use App\Models;
use App\ProductAttribute;
use Illuminate\Http\JsonResponse;
use App\Http\Resources\SettingResource;
use App\Http\Resources\ChannelResource;
use App\Http\Resources\PageResource;
use App\Http\Resources\QuestionResource;
use App\Http\Resources\MemberResource;
use App\Http\Resources\ConsultResource;
use App\Http\Resources\NotificationResource;

use App\Notification;
use App\Channel;
use App\Http\Requests\Api\MemberRegistRequest;
use App\Http\Requests\Api\ConsultRegistRequest;
use App\Http\Requests\Api\LoginRequest;
use App\Http\Requests\Api\EditProfileRequest;
use App\Http\Requests\Api\ForgetRequest;
use App\Http\Requests\Api\ResetRequest;
use App\Http\Requests\Api\VerifyRequest;
use Illuminate\Http\Exceptions\HttpResponseException;


class ProfileController extends Controller
{
    
    
     public function singlechat(Request $request,$id) :JsonResponse
    {
        $user = \Auth::user();
        $channel = Channel::with(['consult','category','typing','member'])->where('id',$id)->first();
        if(!$channel){
           
            if(getCurrentLocale() == 'ar')
            {
                $msg = 'غير موجود';
            }
            else
            {
                $msg = 'Not Exist';
            }
             return response()->json(['message'=>$msg,'data'=>''],200);
         
        }
        
        
        return response()->json(['message'=>'','data'=>new ChannelResource($channel)],200);
    }
     public function chats(Request $request) :JsonResponse
    {
        $user = \Auth::user();
        if($user->role == 'member'){
        $channels = Channel::with(['consult','category','typing'])->where('user_id',$user->id)->where('status','accept')->get();
        $still = Channel::with(['consult','category','typing'])->where('user_id',$user->id)->where('status','proccess')->get();
        
        $pervious = Channel::with(['consult','category','typing'])->where('user_id',$user->id)->where('status','finished')->get();
        }
        else{
             $channels = Channel::with(['consult','category','typing','member'])->where('target_id',$user->id)->where('status','accept')->get();
        $still = Channel::with(['consult','category','typing'])->where('user_id',$user->id)->where('status','proccess')->get();
        
        $pervious = Channel::with(['consult','category','typing','member'])->where('target_id',$user->id)->where('status','finished')->get();
        }
        return response()->json(['message'=>'','data'=>['current'=>ChannelResource::collection($channels),'proccess'=>ChannelResource::collection($still),'pervious'=>ChannelResource::collection($pervious)]],200);
    }
    public function notifications(Request $request) :JsonResponse
    {
        $user = \Auth::user();
        $notifications = Notification::where('user_id',$user->id)->get();
        
        return response()->json(['message'=>'','data'=>['notifications'=>NotificationResource::collection($notifications)]],200);
    }
    public function profile(Request $request){
        
        if(\Auth::user()->role == 'consult'){
        $user = User::with(['notifications','country','category','rates','experience','certification'])->Find(\Auth::user()->id);
        
        return response()->json(['message'=>'','data'=>new ConsultResource($user)],200);
        }else{
            $user = User::with(['notifications','type','currents','pervious'])->Find(\Auth::user()->id);
        
         return response()->json(['message'=>'','data'=>new MemberResource($user)],200);
        }
    }
    public function consultprofile(Request $request,$id){
        $user = User::Find($id);
        
        if(!$user){
            if(getCurrentLocale() == 'ar')
            {
                $msg = 'غير موجود';
            }
            else
            {
                $msg = 'Not Exist';
            }
             return response()->json(['message'=>$msg,'data'=>''],200);
        }
        if($user->active == '0'){
            if(getCurrentLocale() == 'ar')
            {
                $msg = 'غير موجود';
            }
            else
            {
                $msg = 'Not Exist';
            }
             return response()->json(['message'=>$msg,'data'=>''],200);
        }
        if($user->role == 'consult'){
        $user = User::with(['country','category','rates','experience','certification'])->find($id);
        
        return response()->json(['message'=>'','data'=>new ConsultResource($user)],200);
        }else{
            $user = User::with(['type'])->find($id);
        
         return response()->json(['message'=>'','data'=>new MemberResource($user)],200);
        }
    }
    
    
    public function editProfile(EditProfileRequest $request) :JsonResponse
    {
        $user = \Auth::user();
        if(!is_null($request->photo)){
        $name = time().rand(11111,99999).'.' . explode('/', explode(':', substr($request->photo, 0, strpos($request->photo, ';')))[1])[1];

            \Image::make($request->photo)->save(public_path('uploads/images/').$name);
            $request->merge(['photo'=> '/uploads/images/'.$name]);
        }
        if(!is_null($request->password)){
        $request->merge(['password'=>bcrypt($request->password)]);
        }
        else{
            unset($request['password']);
        }
        if(!is_null($request->gender)){
        if($request->gender == 'male'){
            $request->merge(['gender'=>'1']);
        }
        else{
            $request->merge(['gender'=>'0']);
        }
        }
        $user->update($request->all());
        Experience::where('user_id',$user->id)->delete();
        Certification::where('user_id',$user->id)->delete();
        if(!is_null($request->experience)){
        foreach ($request->experience as $key=>$row) 
        {
            
            
           
            $data=[
                'user_id'=> $user->id,
                'image'=> $row['image'],
                    ];
            $experience = Experience::create($data);
            
                foreach (languages() as $lang) 
                    {
                    $data_desc=[
                        'experience_id'=> $experience->id,
                        'language_id'=> $lang->id,
                        'name'=> $row['name_'.$lang->local],
                        
                                ];
                    ExperienceDescription::create($data_desc);
                    }
        }
        }
                if(!is_null($request->certification)){
        foreach ($request->certification as $key=>$row) 
        {
            
            
            $data=[
                'user_id'=> $user->id,
                'image'=> $row['image'],
                    ];
            $certification = Certification::create($data);
             
                foreach (languages() as $lang) 
                    {
                    $data_desc=[
                        'certification_id'=> $certification->id,
                        'language_id'=> $lang->id,
                        'name'=> $row['name_'.$lang->local],
                        
                                ];
                    CertificationDescription::create($data_desc);
                    }
                
        
        
        }
                }
        if(\Auth::user()->role == 'consult'){
        $user = User::with(['notifications','country','category','rates','experience','certification'])->Find(\Auth::user()->id);
        
        return response()->json(['message'=>'','data'=>new ConsultResource($user)],200);
        }else{
            $user = User::with(['notifications','type'])->Find(\Auth::user()->id);
        
         return response()->json(['message'=>'','data'=>new MemberResource($user)],200);
        }
    }

    
   
    
}
