<?php
/**
 * API: Update Loyalty Points Settings
 * Updates the loyalty points system configuration
 */

header('Content-Type: application/json');
session_start();
include_once('../../includes/config.php');

// Check if user is logged in
if (!isset($_SESSION['uid'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

// Get POST data
$data = json_decode(file_get_contents('php://input'), true);

$pointsPer100 = isset($data['points_per_100']) ? floatval($data['points_per_100']) : null;
$redemptionRate = isset($data['redemption_rate']) ? floatval($data['redemption_rate']) : null;
$minRedemption = isset($data['min_redemption_points']) ? intval($data['min_redemption_points']) : null;
$expiryDays = isset($data['points_expiry_days']) ? intval($data['points_expiry_days']) : null;

// Validate inputs
if ($pointsPer100 === null || $redemptionRate === null || $minRedemption === null || $expiryDays === null) {
    echo json_encode(['success' => false, 'message' => 'All settings are required']);
    exit();
}

if ($pointsPer100 < 0 || $redemptionRate < 0 || $minRedemption < 0 || $expiryDays < 0) {
    echo json_encode(['success' => false, 'message' => 'Settings cannot be negative']);
    exit();
}

try {
    mysqli_begin_transaction($con);
    
    // Update points_per_100
    $query1 = "UPDATE tblsettings_loyalty SET SettingValue = ? WHERE SettingKey = 'points_per_100'";
    $stmt1 = mysqli_prepare($con, $query1);
    mysqli_stmt_bind_param($stmt1, 's', $pointsPer100);
    
    // Update redemption_rate
    $query2 = "UPDATE tblsettings_loyalty SET SettingValue = ? WHERE SettingKey = 'redemption_rate'";
    $stmt2 = mysqli_prepare($con, $query2);
    mysqli_stmt_bind_param($stmt2, 's', $redemptionRate);
    
    // Update min_redemption_points
    $query3 = "UPDATE tblsettings_loyalty SET SettingValue = ? WHERE SettingKey = 'min_redemption_points'";
    $stmt3 = mysqli_prepare($con, $query3);
    mysqli_stmt_bind_param($stmt3, 's', $minRedemption);
    
    // Update points_expiry_days
    $query4 = "UPDATE tblsettings_loyalty SET SettingValue = ? WHERE SettingKey = 'points_expiry_days'";
    $stmt4 = mysqli_prepare($con, $query4);
    mysqli_stmt_bind_param($stmt4, 's', $expiryDays);
    
    // Execute all updates
    $success = mysqli_stmt_execute($stmt1) && 
               mysqli_stmt_execute($stmt2) && 
               mysqli_stmt_execute($stmt3) && 
               mysqli_stmt_execute($stmt4);
    
    if ($success) {
        mysqli_commit($con);
        
        echo json_encode([
            'success' => true,
            'message' => 'Loyalty settings updated successfully',
            'settings' => [
                'points_per_100' => $pointsPer100,
                'redemption_rate' => $redemptionRate,
                'min_redemption_points' => $minRedemption,
                'points_expiry_days' => $expiryDays
            ]
        ]);
    } else {
        mysqli_rollback($con);
        echo json_encode(['success' => false, 'message' => 'Failed to update settings']);
    }
    
} catch (Exception $e) {
    mysqli_rollback($con);
    echo json_encode([
        'success' => false,
        'message' => 'Error updating settings: ' . $e->getMessage()
    ]);
}
?>
