<?php
require __DIR__ . '/bootstrap.php';

use App\Core\Auth;
use App\Core\Csrf;
use App\Core\Response;
use App\Services\PhotoVotingService;

use App\Services\NotificationService;
if ($_SERVER['REQUEST_METHOD'] !== 'POST') Response::error('Unsupported method.', 405);
$user = Auth::requireUser();
Csrf::validateFromRequest();
$input = request_input();
if (empty($input['photo_id'])) Response::error('photo_id is required.');
try {
    $photoId = (int)$input['photo_id'];
    (new PhotoVotingService())->vote($photoId, (int)$user['id']);
    (new NotificationService())->notifyVoteCast($photoId, (int)$user['id']);
    Response::ok([], 'Vote registered.');
} catch (Throwable $e) {
    Response::error($e->getMessage());
}
