<?php

session_start();

require __DIR__ . '/vendor/autoload.php';

$config = include __DIR__ . '/config.php';
$internal_pics_dir = realpath(__DIR__ . $config['pics_dir']);
$app = new Phlaym\Roastmonday\Roastmonday($config, $internal_pics_dir);

if (!$app->isAuthenticated()) {
    $url = $app->getAuthURL();
    die('{"authenticated":false, "authUrl": "' . $url . '"}');
}
if (isset($_GET['status'])) {
    $resp = [
        'authenticated' => true,
        'maxPostLength' => $app->getMaxPostLength()
    ];
    die(json_encode($resp));
}
if (empty($_POST['submit'])) {
    $e = ['error' => [
        'message' => 'Unknown action',
    ]];
    die(json_encode($e));
}
$resp = $app->addAvatar(
    $_POST['theme'],
    $_POST['shouldPostAvatar'] ?? false,
    $_FILES['avatar'],
    $_POST['avatarDuration'],
    $_POST['posttext'],
    overwrite_if_exist: true
);
die(json_encode($resp));