<?php

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

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

    $form = new Form($conn, $userId);
    $res = $form->getForms();
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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

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

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

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

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

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

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

?>