<?php
namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

use App\Core\Controller;
use App\Core\Database;

class AuthorSubmissionsController extends Controller
{
    public function submit(): void
    {
        if (!isset($_SESSION['user'])) {
            header("Location: /easychair/public/login");
            exit;
        }

        $this->view('author/submit', []);
    }

    public function submitForm(): void
    {
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            header("Location: /easychair/public/author/submit");
            exit;
        }

        csrf_check();

        $db = Database::getInstance();
        $stmt = $db->prepare("INSERT INTO submissions (title, abstract, user_id, created_at)
                              VALUES (:title, :abstract, :uid, NOW())");

        $stmt->execute([
            ':title'    => $_POST['title'] ?? '',
            ':abstract' => $_POST['abstract'] ?? '',
            ':uid'      => $_SESSION['user']['id']
        ]);

        header("Location: /easychair/public/author/submissions");
        exit;
    }
}
