<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Utilities\ApiCalls;
use Session;
use Illuminate\Support\Facades\Mail;
use App\Mail\NewClientMail;
use App\Models;

class AdminController extends Controller
{
    public function index(){
        $user_id = \Auth::user()->id;
        // dd($user_id);
        $clients_url = "clients?page=0&size=20&sort=createdAt,desc";
        $result = ApiCalls::CurlGet($clients_url);
        $result_arr = json_decode($result);
        // dump($result_arr);
        $data = [
            'page_title' => 'Client List',
            'clients' => $result_arr
        ];
        return view('clients.index', $data);
    }
    public function create() {
        // Show form to create a new post
        return view('clients.create');
    }

    public function store(Request $request) {
        // Save a new post
        $this->validate($request, [
            'name' => 'required',
            'email'=> 'required',
            'phoneNumber' => 'required',
            'country' => 'required',
            // 'status' => 'required',
        ]);
        $client_arr = $request->except('_token');
        

        $create_client_url = "clients";
        $result = ApiCalls::CurlPost(json_encode($client_arr), $create_client_url);
        if ($result) {
            Session::flash('success_message', 'client created successfully!');
            //send email with link to set password and activate their account
            $activation_hash = uniqid();
            $result_arr = json_decode($result, true);
            
            $clients_arr['client_id'] = $result_arr['id'];
            $clients_arr['email'] = $request->email;
            $clients_arr['activation_hash'] = $activation_hash;
            $clients_arr['activation_start_time'] = date('Y-m-d H:i:s');
            $local_result = Models\ClientSession::create($clients_arr);
            \Log::info('Client Session result : ' . json_encode($local_result));
            
            $recipientEmail = $request->email;
            Mail::to($recipientEmail)->send(new NewClientMail($recipientEmail, $activation_hash));
        }
        else{
            Session::flash('error_message', 'client could not be created !');
        }
        return redirect(url('admin/clients'));
    }

    public function show($id) {
        // Show a specific post
        $user_id = \Auth::user()->id;
        $client_url = "clients/1";
        $result = ApiCalls::CurlGet($clients_url);
        $result_arr = json_decode($result);

        $data = [
            'page_title' => 'clients Details',
            'client' => $result_arr
        ];
        // dump($data);
        return view('clients.show', $data);
    }

    public function edit($id) {
        // Show form to edit a post
        $user_id = \Auth::user()->id;
        $result = [];
        $data = [
            'page_title' => 'clients Update',
            'client' => $result
        ];
        return view('clients.edit', $data);
    }

    public function update(Request $request, $id) {
        // Update a specific post
        $this->validate($request, [
            'name' => 'required',
            'email'=> 'required',
            'phone' => 'required',
            'country' => 'country',
            'status' => 'required',
        ]);

        $client = [];
        
        $client->name = $request->name;
        $client->description = $request->description;
        $client->dependancy = $request->dependancy;
        $client->status = $request->status;
        $result = $client->save();
        Session::flash('success_message', 'client details updated successfully!');
        return redirect(url('clients'));
    }

    public function destroy($id) {
        // Delete a specific post
    }
}
