<?php

namespace App\Http\Controllers;

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

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use App\Models\Rating;

class RatingsController extends Controller
{
    public function __construct()
    {
        //
        // $this->middleware('auth:admin');

       // $adminUser = auth()->guard('admin')->user();

        //if (!$adminUser)
        //{
          //  return response()->json(['error' => 'Unauthorized.'], 401);
        //}

    }

    public function save(Request $request)
    {

    	$user_id = $request->user_id;
    	$company_id = $request->company_id;
    	
    	$rating_1 = $request->rating_1;
    	$rating_2 = $request->rating_2;
    	$rating_3 = $request->rating_3;
    	$rating_4 = $request->rating_4;

    	$all_rates = [];

    	$all_rates[] = ['rate' => $rating_1, 'type' => 1, 'user_id' => $user_id, 'company_id' => $company_id];
    	$all_rates[] = ['rate' => $rating_2, 'type' => 2, 'user_id' => $user_id, 'company_id' => $company_id];
    	$all_rates[] = ['rate' => $rating_3, 'type' => 3, 'user_id' => $user_id, 'company_id' => $company_id];
    	$all_rates[] = ['rate' => $rating_4, 'type' => 4, 'user_id' => $user_id, 'company_id' => $company_id];

    	#var_dump($all_rates);die();

    	foreach ($all_rates as $k => $v) 
    	{
    		$check = Rating::where([
    			'user_id', $user_id, 
    			'company_id', $company_id, 
    			'type', $v['type'] 
    		])->first();

    		if (!$check) {
    			$rating = Rating::create([
    				'rate' => $v['rate'],
    				'type' => $v['type'],
    				'user_id' => $user_id,
    				'company_id' =>  $company_id
        		]);

    			var_dump($rating);
    			die('ok');

        		$rating->save();
    		}

    	}

    	$type = $request->type;



    	return response()->json($request);
    }

}
