<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Test extends Controller
{
    //

    public function template(Request $request){
        return 'hi';
    }

//    public function index(Request $request){
//        $user = $request->session()->get('user');
//        return $user;
//    }

    public function index(Request $request){

        //get user details from header
        $api = $request->header('X-API-KEY');
        $userID = $request->header('USER-ID');

        //get users company id, status and rights
        $user = DB::table('agents')->where('agent_id', $userID)->first();
        $companyId = $user->company_id;
        $status = $user->status;

        //get company api key
        $company = DB::table('accounts')->where('account_id',$companyId)->first();
        $company_api = $company->api_key;
        $account_status = $company->account_status;




        if ($company_api != $api || $status != 'active') {
//            return "failed";
            return response()->json(['message' => 'Unauthorized.'], 401);

        }
        return "success";
//            return $next($request);



    }
}
