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

$adminAuth = new AdminAuth($conn);
$pricingManager = new \App\classes\ExternalAPIPricingManager($conn);

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

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

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

// Get form data (only external pricing is allowed)
$id = intval($_POST['id'] ?? 0);

// Validate input
if ($id <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid pricing range ID']);
    exit;
}

// Delete external pricing range
$result = $pricingManager->deleteExternalPriceRange($id);

if ($result) {
    echo json_encode(['success' => true, 'message' => 'External API pricing range deleted successfully']);
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to delete pricing range']);
}

