<?php
/**
 * Admin API — Move Product to Trash
 */
session_start();
header('Content-Type: application/json');

if (empty($_SESSION['admin_logged_in'])) {
    http_response_code(401);
    echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
    exit;
}

require_once '../../includes/config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $productId = $_POST['productId'] ?? null;

    if (!$productId) {
        echo json_encode(['status' => 'error', 'message' => 'Product ID is required.']);
        exit;
    }

    try {
        $stmt = $pdo->prepare("UPDATE products SET is_deleted = 1 WHERE id = ?");
        $stmt->execute([$productId]);
        
        if ($stmt->rowCount() > 0) {
            echo json_encode(['status' => 'success', 'message' => 'Product moved to trash.']);
        } else {
            echo json_encode(['status' => 'error', 'message' => 'Failed to move product to trash.']);
        }
    } catch (PDOException $e) {
        http_response_code(500);
        echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
    }
}
?>
