<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Exception;

class StudentController extends BaseController
{

    //Cours de l'etudiant. Annee en cours comme reprises.

    public function courses(Request $request){

        try {

            // $userId = $request->get('User')->id;

            // $matricule = DB::connection('tenant')
            //     ->table('users')
            //     ->where('id',$userId)
            //     ->get()->first()->matricule;

            $User = $request->get('User');
            $matricule = $User->matricule;

            // $server_response = getFromGest('api/v1/userStudents/'.$matricule.'/ecus', ['bd' => substr($CurrentDb, 10), 'option' => 'all'])->json();
            if ($request->get('version')) {
                $server_response = reqGest('POST','v1/userStudents/'.$matricule.'/ecus', ['option' => 'all'],2);
            } else {
                $server_response = reqGest('GET','v1/students/'.$matricule.'/ecus', [
                    // 'limit' => 6,
                ], 2);
            }
            if( $server_response && $server_response['success'] == true){
                return sendResponse($server_response['data'],$server_response['message']);
            } else {
                return sendError("ERROR_FROM_GEST", [$server_response]);
            }

        } catch (Exception $e) {
            return sendError("SERVER ERROR, PLEASE CONTACT ADMINISTRATOR", $e->getMessage(), 400);
        }

    }

    // Details sur une ECU

    public function ecu_data(Request $request, $ecuId){

        try {
            $toSend =  ["id_0" => secure_me($ecuId,true)];

            $server_response = reqGest('POST','v1/ecus/infos', ['ecuIds' => $toSend],2);
            if($server_response && $server_response['success']){
                $ecus_data = $server_response['data'];
                $ecu = $ecus_data[array_key_first($ecus_data)][0]; // get details of ecu

                return sendResponse([
                    "ecu" => $ecu
                ],'DATA_GOT_SUCCESSFULLY');

            }
            else {

                return sendError($server_response['message']);

            }

        } catch (Exception $e) {

            return sendError("SERVER ERROR, PLEASE CONTACT ADMINISTRATOR", $e->getMessage(), 400);

        }
    }

    // Etudiants ayant une ECU donnee

    public function students_ecu(Request $request, $ecuId){

        try {

            $CurrentDb= $request->get('CurrentDb');

            // $server_response = getFromGest('api/v1/ecus/'.$code_ecu.'/students_ecu', ['bd' => substr($CurrentDb, 10)])->json();
            $server_response = reqGest('GET','v1/ecus/'.$ecuId.'/students_ecu',[],2);

            if(isset($server_response['success']) && $server_response['success'] == true){

                $ecus_data = $server_response['data'];

                return sendResponse($ecus_data,'DATA_GOT_SUCCESSFULLY');

            }
            else if(isset($server_response['success']) && $server_response['success'] == false){

                return sendError($server_response['message']);

            }
        } catch (Exception $e) {
            return sendError("SERVER ERROR, PLEASE CONTACT ADMINISTRATOR", $e->getMessage(), 400);
        }

    }

    public function exam (Request $request) {

        try {
            $User = $request->get('User');

            $server_response = reqGest('POST','v1/userStudents/'.$User->matricule.'/ecus', ['option' => 'notAll'], 2);
            if($server_response && $server_response['success']){

                // $codes_ecus = $server_response['data']['codes_ecus'];
                $ids_ecus = $server_response['data']['ids_ecus'];
                $ecs = $server_response['data']['infos_ecus'];

                // HACK below : Need to solve this later ! use array_merge instead of array_push in the API
                //  Check routes & projects that call this route and update them !
                //  GESTSTUDENTAPI : v1/userStudents/.$matricule./ecus
                $ecs = array_merge($ecs[0], $ecs[1]);

                $server_response = reqGest('POST','v1/userStudents/exam', ['ids_ecus' => $ids_ecus],2);
                if($server_response['success'] && $server_response['success']){

                    $exams = $server_response['data'];

                    return sendResponse([
                        'exams' => $exams,
                        'ecs' => $ecs
                    ],'DATA_GOT_SUCCESSFULLY');
                }  else {
                    return sendError($server_response['message']);
                }
            }  else {

                return sendError($server_response['message']);

            }
        } catch (Exception $e) {
            return sendError("SERVER ERROR, PLEASE CONTACT ADMINISTRATOR", $e->getMessage(), 400);
        }
    }

    public function get_school (Request $request) {

        try {

            $server_response = reqGest('GET','v1/etablissements',[],2);

            if(isset($server_response['success']) && $server_response['success'] == true){

                return sendResponse($server_response['data'],'DATA_GOT_SUCCESSFULLY');

            } else if(isset($server_response['success']) && $server_response['success'] == false){

                return sendError($server_response['message']);

            }

        } catch (Exception $e) {
            return sendError("SERVER ERROR, PLEASE CONTACT ADMINISTRATOR", $e->getMessage(), 400);
        }
    }

}
