<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../model/Article.php';
require_once __DIR__ . '/../model/ArticleManager.php';

// 2. Prüfen, ob eine gültige Artikel-ID übergeben wurde
if (isset($_GET["id"]) && !empty($_GET["id"])) {
    $articleId = intval($_GET["id"]);
    $userEmail = $_SESSION["user_email"];

    if (!isset($_SESSION["user_email"]) || empty($_SESSION["user_email"])) {
        $_SESSION["message"] = "unauthorized_access";
        header("Location: ../../index.php?pfad=showArticle&id=" . $articleId);
        exit();
    }

    try {
        $articleManager = ArticleManager::getInstance();
        $articleManager->toggleLike($articleId, $userEmail);

        header("Location: ../../index.php?pfad=showArticle&id=" . $articleId);
        exit();

    } catch (NotFoundException $e) {
        $_SESSION["message"] = "missing_id";
        header("Location: ../../index.php");
        exit();
    } catch (Exception $e) {
        $_SESSION["message"] = "internal_error";
        header("Location: ../../index.php");
        exit();
    }
} else {
    $_SESSION["message"] = "missing_id";
    header("Location: ../../index.php");
    exit();
}

?>