<?php
require_once __DIR__ . '/../../config/cors.php';
require_once __DIR__ . '/../../config/jwt.php';

$user    = auth_required();
$user_id = (int)($user['id'] ?? 0);

if (!$user_id) { http_response_code(401); echo json_encode(['error' => 'Bad token']); exit; }

$action  = substr(trim($_POST['action']  ?? ''), 0, 50);
$details = substr(trim($_POST['details'] ?? ''), 0, 1000);
$lat     = strlen($_POST['lat'] ?? '') > 0 ? floatval($_POST['lat']) : null;
$lng     = strlen($_POST['lng'] ?? '') > 0 ? floatval($_POST['lng']) : null;

if (empty($action)) {
    http_response_code(400); echo json_encode(['success' => false, 'message' => 'Action required']); exit;
}

$pdo->prepare(
    "INSERT INTO activity_logs (surveyor_id, action, details, latitude, longitude)
     VALUES (?, ?, ?, ?, ?)"
)->execute([$user_id, $action, $details, $lat, $lng]);

echo json_encode(['success' => true]);
