<?php
/**
 * Delete Custom Package Pricing
 */

session_start();
require_once '../config.php';
require_once '../app/classes/StoreManager.php';
require_once '../app/classes/StorePricingManager.php';

header('Content-Type: application/json');

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

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Method not allowed']);
    exit();
}

$userId = $_SESSION['user_id'];
$storeId = intval($_POST['store_id'] ?? 0);
$pricingId = intval($_POST['pricing_id'] ?? 0);

// Verify user owns this store
$storeManager = new StoreManager($conn);
$store = $storeManager->getStoreById($storeId);

if (!$store || $store['owner_id'] != $userId) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

// Verify pricing belongs to this store
$verifyStmt = $conn->prepare("SELECT id FROM store_custom_pricing WHERE id = ? AND store_id = ?");
$verifyStmt->bind_param("ii", $pricingId, $storeId);
$verifyStmt->execute();
$verifyResult = $verifyStmt->get_result();

if ($verifyResult->num_rows === 0) {
    $verifyStmt->close();
    echo json_encode(['success' => false, 'message' => 'Custom pricing not found']);
    exit();
}
$verifyStmt->close();

// Delete
$pricingManager = new StorePricingManager($conn);
$success = $pricingManager->deleteStoreCustomPricing($pricingId);

if ($success) {
    echo json_encode(['success' => true, 'message' => 'Custom pricing deleted']);
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to delete']);
}
?>






