<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../admin/auth.php';
header('Content-Type: application/json');

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

$input = json_decode(file_get_contents('php://input'), true) ?? $_POST;

if (empty($input['name']) && empty($input['id'])) {
    http_response_code(400);
    echo json_encode(['success' => false, 'error' => 'Package name is required']);
    exit;
}

$packages = readJSON(PACKAGES_FILE);
$isNew = empty($input['id']);

if ($isNew) {
    $pkg = [
        'id'          => 'pkg-' . strtoupper(substr(uniqid(), -4)),
        'name'        => trim(htmlspecialchars($input['name'], ENT_QUOTES, 'UTF-8')),
        'badge'       => trim(htmlspecialchars($input['badge'] ?? '', ENT_QUOTES, 'UTF-8')),
        'highlight'   => !empty($input['highlight']),
        'price_label' => trim(htmlspecialchars($input['price_label'] ?? 'Contact for Pricing', ENT_QUOTES, 'UTF-8')),
        'price_note'  => trim(htmlspecialchars($input['price_note'] ?? '', ENT_QUOTES, 'UTF-8')),
        'icon'        => trim(htmlspecialchars($input['icon'] ?? 'fa-store', ENT_QUOTES, 'UTF-8')),
        'color'       => trim(htmlspecialchars($input['color'] ?? '#9AA5B4', ENT_QUOTES, 'UTF-8')),
        'space_sqm'   => intval($input['space_sqm'] ?? 9),
        'features'    => $input['features'] ?? [],
        'cta_label'   => trim(htmlspecialchars($input['cta_label'] ?? 'Enquire Now', ENT_QUOTES, 'UTF-8')),
        'cta_type'    => 'enquiry',
        'sort_order'  => intval($input['sort_order'] ?? count($packages) + 1),
        'active'      => !empty($input['active']),
        'created_at'  => date('Y-m-d')
    ];
    $packages[] = $pkg;
} else {
    $found = false;
    foreach ($packages as &$pkg) {
        if ($pkg['id'] === $input['id']) {
            if (isset($input['name'])) $pkg['name'] = trim(htmlspecialchars($input['name'], ENT_QUOTES, 'UTF-8'));
            if (isset($input['badge'])) $pkg['badge'] = trim(htmlspecialchars($input['badge'], ENT_QUOTES, 'UTF-8'));
            if (isset($input['highlight'])) $pkg['highlight'] = !empty($input['highlight']);
            if (isset($input['price_label'])) $pkg['price_label'] = trim(htmlspecialchars($input['price_label'], ENT_QUOTES, 'UTF-8'));
            if (isset($input['price_note'])) $pkg['price_note'] = trim(htmlspecialchars($input['price_note'], ENT_QUOTES, 'UTF-8'));
            if (isset($input['icon'])) $pkg['icon'] = trim(htmlspecialchars($input['icon'], ENT_QUOTES, 'UTF-8'));
            if (isset($input['color'])) $pkg['color'] = trim(htmlspecialchars($input['color'], ENT_QUOTES, 'UTF-8'));
            if (isset($input['space_sqm'])) $pkg['space_sqm'] = intval($input['space_sqm']);
            if (isset($input['features'])) $pkg['features'] = $input['features'];
            if (isset($input['cta_label'])) $pkg['cta_label'] = trim(htmlspecialchars($input['cta_label'], ENT_QUOTES, 'UTF-8'));
            if (isset($input['sort_order'])) $pkg['sort_order'] = intval($input['sort_order']);
            if (isset($input['active'])) $pkg['active'] = !empty($input['active']);
            $found = true;
            break;
        }
    }
    if (!$found) {
        http_response_code(404);
        echo json_encode(['success' => false, 'error' => 'Package not found']);
        exit;
    }
}

writeJSON(PACKAGES_FILE, $packages);
echo json_encode(['success' => true, 'package' => $pkg, 'isNew' => $isNew]);
exit;
