<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Http\Requests\LoginIndexRequest;
use App\Http\Requests\LoginRequest;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class WelcomeController extends Controller
{
    public function index(LoginIndexRequest $request)
    {
        return view('v1.connexion')->with([
            'BD' => $request->BD,
            'CALLBACK_URL' => $request->CALLBACK_URL
        ]);
    }

    public function login(LoginRequest $request)
    {
        try {
            $try_login_user = Http::withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
                'Authorization' => '2131566549'
            ])->post(route('user_login'), [
                'bd' => $request->BD,
                'username' => $request->username,
                'password' => $request->password
            ]);

            $try_login_student_user = Http::withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
                'Authorization' => '2131566549'
            ])->post(route('user_student_login'), [
                'bd' => $request->BD,
                'username' => $request->username,
                'password' => $request->password
            ]);

            if ($try_login_user->json()['success']) {
                return redirect()->away(
                    $request->CALLBACK_URL .
                        "?status=SUCCESS&message=" .
                        $try_login_user->json()['message'] . "&access_token=" .
                        $try_login_user->json()['data']['access_token'] . "&matricule=" .
                        $try_login_user->json()['data']['user_matricule'] . "&type_user=" .
                        $try_login_user->json()['data']['type_user'] . "&expires_at=" .
                        $try_login_user->json()['data']['expires_at']
                );
            } else if ($try_login_student_user->json()['success']) {
                return redirect()->away(
                    $request->CALLBACK_URL .
                        "?status=SUCCESS&message=" .
                        $try_login_student_user->json()['message'] . "&access_token=" .
                        $try_login_student_user->json()['data']['access_token'] . "&matricule=" .
                        $try_login_student_user->json()['data']['user_student_matricule'] . "&type_user=" .
                        $try_login_student_user->json()['data']['type_user'] . "&expires_at=" .
                        $try_login_student_user->json()['data']['expires_at']
                );
            } else {
                return view('v1.connexion')->with([
                    'BD' => $request->BD,
                    'CALLBACK_URL' => $request->CALLBACK_URL,
                    'error' => "Echec d'authentification !"
                ]);
            }
        } catch (Exception $e) {
            return redirect()->away($request->CALLBACK_URL . "?status=ERROR&message=" . $e->getMessage());
        }
    }

    public function callback(Request $request)
    {
        try {
            if ($request->status == 'SUCCESS') {
                $authenticate = Http::withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json',
                    'Authorization' => '744fa703c6503549749a'
                ])->post('http://localhost/ProjetsLaravel/DepProjet/projetApi/public/api/v1/user/auth', [
                    'access_token' => $request->access_token,
                    'matricule' => $request->matricule,
                    'type_user' => $request->type_user,
                    'expires_at' => $request->expires_at
                ]);

                dd($authenticate->json());
            } elseif ($request->status == 'FAILED') {
                dd($request);
            } else {
                dd($request);
            }
        } catch (Exception $e) {
            dd($e);
        }
    }
}
