<?php
/**
 * Add Custom Pricing for Specific Package
 */

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);
$packageSize = trim($_POST['package_size'] ?? '');
$cost = floatval($_POST['cost'] ?? 0);
$network = $_POST['network'] ?? 'mtn';

// 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 - You do not own this store']);
    exit();
}

// Validation
if (empty($packageSize)) {
    echo json_encode(['success' => false, 'message' => 'Package size is required']);
    exit();
}

if ($cost <= 0) {
    echo json_encode(['success' => false, 'message' => 'Price must be greater than 0']);
    exit();
}

// Add custom pricing
$pricingManager = new StorePricingManager($conn);
$result = $pricingManager->setStoreCustomPricing($storeId, $packageSize, $cost, $network);

echo json_encode($result);
?>






