<?php

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

$app->post('/users', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $user = new User($conn);
    $res = $user->registerUser($request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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

$app->post('/users/login', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $user = new User($conn);
    $res = $user->tryLogin($request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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

$app->get('/users', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $user = new User($conn);
    $res = $user->getUsers($request->getParams());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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

$app->get('/users/{id}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $user = new User($conn);
    $id = $args['id'];
    $res = $user->getUser($id);
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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


$app->put('/users/{id}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

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

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

$app->put('/users/permission/{id}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

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

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

$app->delete('/users/{id}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $user = new User($conn);
    $id = $args['id'];
    $res = $user->deleteUser($id);
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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


$app->post('/signin', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $userToken = $request->getHeader("Authorization")[0];
    $user = new User($conn, null);
    $res = $user->signInUser($request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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

$app->get('/signin/{workingDate}', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $userToken = $request->getHeader("Authorization")[0];
    $user = new User($conn, null);
    $res = $user->getSigninDatas($args['workingDate']);
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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

$app->post('/signin/searchsigin', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $userToken = $request->getHeader("Authorization")[0];
    $user = new User($conn, null);
    $res = $user->searchAttendance($request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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


$app->post('/users/landing', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();

    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $user = new User($conn, null);
    $res = $user->checkLanding($userId);
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

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


?>