<?php

namespace App\Http\Controllers;

use App\Lib\Backend;
use App\Models\Meeting;
use App\Models\MeetingParticipant;
use Illuminate\Http\Request;
use App\Models\Pro;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;

class GuestController extends Controller
{

    public function join(Request $request) {
        return view('join');
    }

    public function processJoin(Request $request) {

        $api = new Backend();

        try {

            $apiResponse = $api->post('client/createAsGuest', [
                'nameFirst' => $request->post('nameFirst'),
                'nameLast' => $request->post('nameLast'),
                'dob' => $request->post('dob'),
                'medicareNumber' => $request->post('medicareNumber'),
                'phoneNumber' => $request->post('phoneNumber'),
                'emailAddress' => $request->post('emailAddress'),
            ]);

            $data = json_decode($apiResponse->getContents());

            if (!property_exists($data, 'success') || !$data->success) {
                return back()->with('message', $data->message)
                    ->withInput($request->input());
            }

            Cookie::queue('sessionKey', $data->data->sessionKey);
            Cookie::queue('clientUid', $data->data->clientUid);

            return redirect('/client/dashboard');

        } catch (\Exception $e) {
            return redirect()->back()->with('message', $e->getMessage());
        }
    }

    public function checkin(Request $request) {
        return view('checkin');
    }

    public function processCheckin(Request $request) {

        $api = new Backend();

        try {

            $apiResponse = $api->post('session/clientCheckInTokenLogIn', [
                'checkInToken' => $request->post('checkInToken'),
                'dob' => $request->post('dob'),
            ]);

            $data = json_decode($apiResponse->getContents());

            if (!property_exists($data, 'success') || !$data->success) {
                return back()->with('message', $data->message)
                    ->withInput($request->input());
            }

            Cookie::queue('sessionKey', $data->data->sessionKey);
            Cookie::queue('clientUid', $data->data->clientUid);

            return redirect('/client/dashboard');

        } catch (\Exception $e) {
            return redirect()->back()->with('message', $e->getMessage());
        }
    }

    public function meet(Request $request) {
        return view('meet', [
            'guest' => true
        ]);
    }

    public function dashboard(Request $request) {
        $clientUid = $request->cookie('clientUid');
        $client = DB::table('client')->where('uid', $clientUid)->first();
        return view('client-dashboard', [
            "client" => $client
        ]);
    }

    public function getCheckinToken(Request $request, $uid) {
        $client = DB::table('client')->where('uid', $uid)->first();
        return json_encode([
            "data" => $client->check_in_token
        ]);
    }
}
