<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Exception;

class AuthController extends Controller
{
    public function auth(Request $request)
    {
    
    	try {
    	//id matricule access_token refresh_token expire_at last_login_time type_user remember_token created_at updated_at
      	    DB::connection('tenant')->table('users')
								->updateOrInsert(
										['matricule' => $request->matricule],
										['access_token' => $request->access_token, "refresh_token" => $request->refresh_token, 'expire_at' => $request->expire_at, 'last_login_time' => $request->last_login_time, 'type_user' => $request->type_user]
								);
			$user = DB::connection('tenant')->table('users')->where("matricule", $request->matricule)->first();
			$success = [];
			$success['user_id'] = $user->id;
			$success['user_authorization'] = $user->user_authorization;
			
            return sendResponse($success, "DATA_INSERTED_SUCCESSFULLY");
      } catch (Exception $e) {
            return sendError("SERVER ERROR, PLEASE CONTACT ADMINISTRATOR", $e->getMessage(), 400);
      }
      
    }
    
    // public function check_auth(Request $request)
    // {
    // 	try {
    //   	    $check_token = DB::table('login_history')->where('access_token', $request->access_token)->first();

	// 		if ($check_token) { // normally, here get user profile
	// 			if ($check_token->type_user == 0) {
	// 				$info = DB::connection('tenant')->table('tabinscription')->select('idFiliere', 'annee', 'niveauFormation')
	// 							->where('matricule', $check_token->matricule)->first();
	// 			} else {
	// 				$info = [];
	// 			}
	// 			return sendResponse($info, "DATA_GOT_SUCCESSFULLY");
	// 		}
	// 		return sendError("WRONG ACCESS TOKEN");
	// 	} catch (Exception $e) {
	// 			return sendError("SERVER ERROR, PLEASE CONTACT ADMINISTRATOR", $e->getMessage(), 400);
	// 	}

    // }

    
}
