<?php
// ajax-share.php
require_once 'includes/db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['product_id']) && isset($_POST['platform'])) {
    $product_id = (int)$_POST['product_id'];
    $platform = $_POST['platform']; // whatsapp, facebook, twitter, copy
    $ip_address = $_SERVER['REMOTE_ADDR'];

    try {
        global $db;
        $stmt = $db->prepare("INSERT INTO product_shares (product_id, platform, ip_address) VALUES (?, ?, ?)");
        $stmt->execute([$product_id, $platform, $ip_address]);
        
        echo json_encode(['status' => 'success']);
    } catch (PDOException $e) {
        echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
    }
}
?>