<?php
session_start();
require_once '../../config.php';
require_once '../../app/classes/AdminAuth.php';

$adminAuth = new AdminAuth($conn);

// Check if admin is authenticated
if (!$adminAuth->isAuthenticated()) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
    exit();
}

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

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    echo json_encode(['success' => false, 'message' => 'Invalid pricing ID']);
    exit();
}

$pricingId = intval($_GET['id']);

try {
    $stmt = $conn->prepare("SELECT * FROM custom_pricing WHERE id = ?");
    $stmt->bind_param("i", $pricingId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $pricing = $result->fetch_assoc();
        echo json_encode([
            'success' => true,
            'pricing' => $pricing
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Pricing not found'
        ]);
    }
    
    $stmt->close();
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error fetching pricing details: ' . $e->getMessage()
    ]);
}
