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

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

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

if (!$userData || $userData['role'] !== 'admin') {
    Helpers::sendResponse(false, 'Unauthorized. Admin access required.', null, 403);
}

$data = json_decode(file_get_contents("php://input"));

// Validate required fields
if (empty($data->service_id)) {
    Helpers::sendResponse(false, 'Service ID is required', null, 400);
}

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

// Build update query dynamically
$updateFields = [];
$params = [':service_id' => $data->service_id];

if (isset($data->name)) {
    $updateFields[] = "name = :name";
    $params[':name'] = $data->name;
}

if (isset($data->description)) {
    $updateFields[] = "description = :description";
    $params[':description'] = $data->description;
}

if (isset($data->location)) {
    $updateFields[] = "location = :location";
    $params[':location'] = $data->location;
}

if (isset($data->latitude)) {
    $updateFields[] = "latitude = :latitude";
    $params[':latitude'] = $data->latitude;
}

if (isset($data->longitude)) {
    $updateFields[] = "longitude = :longitude";
    $params[':longitude'] = $data->longitude;
}

if (isset($data->geofence_radius)) {
    $updateFields[] = "geofence_radius = :geofence_radius";
    $params[':geofence_radius'] = $data->geofence_radius;
}

if (isset($data->start_time)) {
    $updateFields[] = "start_time = :start_time";
    $params[':start_time'] = $data->start_time;
}

if (isset($data->end_time)) {
    $updateFields[] = "end_time = :end_time";
    $params[':end_time'] = $data->end_time;
}

if (isset($data->is_reward_based)) {
    $updateFields[] = "is_reward_based = :is_reward_based";
    $params[':is_reward_based'] = $data->is_reward_based;
}

if (isset($data->tickets_required)) {
    $updateFields[] = "tickets_required = :tickets_required";
    $params[':tickets_required'] = $data->tickets_required;
}

if (isset($data->is_active)) {
    $updateFields[] = "is_active = :is_active";
    $params[':is_active'] = $data->is_active;
}

if (empty($updateFields)) {
    Helpers::sendResponse(false, 'No fields to update', null, 400);
}

$query = "UPDATE services SET " . implode(', ', $updateFields) . " WHERE id = :service_id";
$stmt = $db->prepare($query);

foreach ($params as $key => $value) {
    $stmt->bindValue($key, $value);
}

if ($stmt->execute()) {
    // Log admin activity
    $query = "INSERT INTO admin_logs (admin_id, action, target_type, target_id, details) 
              VALUES (:admin_id, 'update_service', 'service', :service_id, :details)";

    $details = json_encode($params);
    $stmt = $db->prepare($query);
    $stmt->bindParam(':admin_id', $userData['user_id']);
    $stmt->bindParam(':service_id', $data->service_id);
    $stmt->bindParam(':details', $details);
    $stmt->execute();

    Helpers::sendResponse(true, 'Service updated successfully');
} else {
    Helpers::sendResponse(false, 'Service update failed', null, 500);
}
