<?php
require_once 'config/database.php';
require_once 'utils/helpers.php';

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    Helpers::sendResponse(false, 'Method not allowed', null, 405);
}

// Get and verify token
$token = Helpers::getBearerToken();
$userData = Helpers::verifyJWT($token);

if (!$userData) {
    Helpers::sendResponse(false, 'Unauthorized', null, 401);
}

$database = new Database();
$db = $database->getConnection();

// Get active service for today
$today = date('Y-m-d');
$now = date('Y-m-d H:i:s');

$query = "SELECT id, name, description, location, latitude, longitude, geofence_radius, 
                 start_time, end_time, is_reward_based, tickets_required
          FROM services 
          WHERE is_active = TRUE 
          AND DATE(start_time) = :today
          AND start_time >= :now 
          ORDER BY start_time ASC
          LIMIT 1";

$stmt = $db->prepare($query);
$stmt->bindParam(':today', $today);
$stmt->bindParam(':now', $now);
$stmt->execute();

if ($stmt->rowCount() > 0) {
    $service = $stmt->fetch();
    Helpers::sendResponse(true, 'Active service found', [
        'service' => $service
    ]);
} else {
    Helpers::sendResponse(true, 'No active service', [
        'service' => null
    ]);
}
