<?php

namespace App\Http\Controllers\Api\v2;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Exceptions\GeneralException;
use App\Http\Controllers\Controller;

use App\Models\Category;
use App\Models\UserCalender;
use App\Models\Symptom;
use App\Models\CategorySymptom;
use Carbon\Carbon;


class ApiSymptomsController extends Controller
{
    

    public function getSymptomsOptions(Request $request)
    {
        
        $response = array();
        $output = array();
            
                $category['category'] = Category::select('id', 'name')->get();


                if(count($category['category'])>0)
                {
                    foreach ($category['category'] as $key => $value) 
                    {
                        
                        $category['category'][$key]['symptoms'] = Symptom::select('id', 'cat_id', 'text', 'icon', 'disable_id')->where('cat_id', $value->id)->get();                     
                    }

                    $status = 200;
                    $statusCode = 200;
                    $message = "All Categories And Symptoms";
                    $output = $category;
                }
                else
                {
                    $status = 201;
                    $statusCode = 201;
                    $message = "No Questions Available";
                }
                
            
        
        $response = array('status'=>$status, 'message'=>$message, 'result'=>$output);
        
        return response()->json($response);
        
    }


    public function submitSymptons(Request $request)
    {
        $response = array();
        $output = array();

        $inputs = $request->all();
        $inputs = getTrimmedValue($inputs);

        $user_id = trim($request->input('user_id'));
        $date = trim(date('Y-m-d', strtotime($request->date)));
        $symptom_note = trim($request->input('symptom_note'));
        $symptoms = trim($request->input('symptom_id'));

        $curr_date = Date('Y-m-d');
        
        if (strpos($symptoms, ",") !== false) 
        {
		    $symptom_id = explode(',', $symptoms);
		}
		else
		{
			$symptom_id[0] = $symptoms;
		}

        // dd($symptom_id);
        if($user_id && $date && $symptom_id[0] != '')
        {
            $user_category_symptom_exist = CategorySymptom::where(['user_id'=> $user_id, 'date'=> $date])->get();

            // if($date <= $curr_date)
            // {

                if(count($user_category_symptom_exist) == 0)
                {       

                       foreach ($symptom_id as $key => $value) 
                        {
                            $symptom = Symptom::find($value);
                            // $date = parse_str($date);
                            $calender = UserCalender::where('user_id',$user_id)->whereDate('sdate', '<=', $date)->whereDate('edate', '>=', $date)->first();

                            // dd($calender);
                            
                            if($calender)
                            {
                                if($symptom_note != '')
                                {
                                    $insert = CategorySymptom::insert([
                                    'user_id' => $user_id,
                                    'calender_id' => $calender->id,
                                    'date' => $date,
                                    'cat_id' => $symptom->cat_id,
                                    'symptom_id' => $value,
                                    'symptom' => $symptom->text,
                                    'symptoms_note' => $symptom_note,
                                    'icon' => $symptom->icon,
                                    ]);    
                                }
                                else
                                {
                                     $insert = CategorySymptom::insert([
                                    'user_id' => $user_id,
                                    'calender_id' => $calender->id,
                                    'date' => $date,
                                    'cat_id' => $symptom->cat_id,
                                    'symptom_id' => $value,
                                    'symptom' => $symptom->text,
                                    'symptoms_note' => '',
                                    'icon' => $symptom->icon,
                                    ]);
                                }
                                    
                            }
                            else
                            {
                                $insert = false;
  
                            }                                
                              
                        }
                        

                        if($insert)
                        {
                        	$user_category_symptom_exist = CategorySymptom::select('cat_id', 'symptom_id', 'symptom', 'icon', 'symptoms_note')->where(['user_id'=> $user_id, 'date'=> $date])->get();

                        	$status = 200;
                            $statusCode = 200;
                            $message = "Data Insert Successful";
                            $output = $user_category_symptom_exist; 
                        }
                        else
                        {
                        	$status = 201;
                            $statusCode = 201;
                            $message = "Data Not Insert Successful";
                        }  
                }
                else
                {
                	$delete = CategorySymptom::where(['user_id'=> $user_id, 'date'=> $date])->delete();

                     foreach ($symptom_id as $key => $value) 
                    {
                    	// $check = CategorySymptom::where('symptom_id', $value)->get();
                    	
                    		$symptom = Symptom::find($value);

                            $calender = UserCalender::where('user_id',$user_id)->whereDate('sdate', '<=', $date)->whereDate('edate', '>=', $date)->first();
                            
                            if($calender)
                            {
                                if($symptom_note != '')
                                {
                                    $update = CategorySymptom::insert([
                                            'user_id' => $user_id,
                                            'calender_id' => $calender->id,
                                            'date' => $date,
                                            'cat_id' => $symptom->cat_id,
                                            'symptom_id' => $value,
                                            'symptom' => $symptom->text,
                                            'symptoms_note' => $symptom_note,
                                            'icon' => $symptom->icon,
                                        ]);
                                }
                                else
                                {
                                    $update = CategorySymptom::insert([
                                            'user_id' => $user_id,
                                            'calender_id' => $calender->id,
                                            'date' => $date,
                                            'cat_id' => $symptom->cat_id,
                                            'symptom_id' => $value,
                                            'symptom' => $symptom->text,
                                            'symptoms_note' => '',
                                            'icon' => $symptom->icon,
                                        ]);
                                }
        	                    
                            }
                            else
                            {
                                $update = false;
                            } 
                    	
                          
                    }
                    // dd($update);

                            if($update)
                            {
                            	$user_category_symptom_exist = CategorySymptom::select('cat_id', 'symptom_id', 'symptom', 'icon', 'symptoms_note')->where(['user_id'=> $user_id, 'date'=> $date])->get();

                                $status = 200;
                                $statusCode = 200;
                                $message = "Data Update Succesfully";
                                $output = $user_category_symptom_exist;
                            }
                            else
                            {
                                $status = 201;
                                $statusCode = 201;
                                $message = "Already Updated";
                            }
                }
            // }
            // else
            // {
            //     $status = 201;
            //     $statusCode = 201;
            //     $message = "Please Enter Present Or Previous Date";
            // }
                
        }
        else
        {
            $status = 201;
            $statusCode = 201;
            $message = "Please Fill The Required Fields";
        }

        $response = array('status'=>$status, 'message'=>$message, 'result'=>$output);
        
        return response()->json($response);
            
            
    }	

}
