<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Facades\JWTFactory;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Tymon\JWTAuth\PayloadFactory;
use Tymon\JWTAuth\JWTManager as JWT;

class AuthController extends Controller
{
    public function register(Request $request)
    {
        if ($request->json()->get('role') == 1) {
            $validator = Validator::make($request->json()->all() , [
                'first_name' => 'required|string|max:255',
                'last_name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:users,email_users',
                'password' => 'required|string|min:8|confirmed',
            ]);
    
            if($validator->fails()) {
                return response()->json($validator->errors()->toJson(), 400);
            }
    
            $user = User::create([
                'name' => $request->json()->get('first_name')." ".$request->json()->get('last_name'),
                'password' => Hash::make($request->json()->get('password')),
                'email_users' => $request->json()->get('email'),
            ]);
        } else {
            $validator = Validator::make($request->json()->all() , [
                'company_name' => 'required|string|max:255',
                'username' => 'required|string|max:255|unique:users',
                'email' => 'required|string|email|max:255|unique:users,email_employers',
                'password' => 'required|string|min:8|confirmed',
                'tel' => 'required|string|max:255',
            ]);

            if($validator->fails()) {
                return response()->json($validator->errors()->toJson(), 400);
            }

            $user = User::create([
                'company_name' => $request->json()->get('company_name'),
                'password' => Hash::make($request->json()->get('password')),
                'username' => $request->json()->get('username'),
                'tel' => $request->json()->get('tel'),
                'role' => 2,
                'email_employers' => $request->json()->get('email'),
            ]);
        }

        $token = JWTAuth::fromUser($user);

        return response()->json(compact('user', 'token'), 201);
    }

    public function login(Request $request)
    {
        $credentials = $request->json()->all();

        try {
            if(! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 400);
            }
        } catch(JWTException $e) {
            return response()->json(['error' => 'could_not_create_token', 500]);
        }
        $date = date();

        return response()->json(compact('token', 'date'));
    }

    public function getAuthenticatedUser()
    {
        try {
            if(! $user = JWTAuth::parseToken()->authenticate()) {
                return response()->json(['user_not_found'], 404);
            }
        } catch(Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
            return response()->json(['token_expired'], $e->getStatusCode());
        } catch(Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
            return response()->json(['token_invalid', $e->getStatusCode()]);
        } catch(Tymon\JWTAuth\Exceptions\JWTException $e) {
            return response()->json(['token_absent'], $e->getStatusCode());
        }

        return response()->json(compact('user'));
    }
}
