<?php

require_once '../class/Meeting.php';
require_once '../class/Security.php';

$app->get('/meetings', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $meeting = new Meeting($conn, $userId);
    $res = $meeting->getMeetings($request->getParams());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});

$app->get('/meeting', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $meeting = new Meeting($conn, $userId);
    $res = $meeting->getMeeting($request->getParams());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});

$app->post('/meetings', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $meeting = new Meeting($conn, $userId);
    $res = $meeting->saveMeeting($request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});

$app->post('/meetings/agendas', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $meeting = new Meeting($conn, $userId);
    $res = $meeting->saveAgenda($request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});

$app->put('/meetings/agendas/{id}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $meeting = new Meeting($conn, $userId);
    $res = $meeting->updateAgenda($args['id'], $request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});


$app->put('/meeting/{id}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $meeting = new Meeting($conn, $userId);
    $res = $meeting->updateMeeting($args['id'], $request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});

$app->delete('/meeting/{id}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $meeting = new Meeting($conn, $userId);
    $res = $meeting->deleteMeeting($args['id']);
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});


?>