<?php
session_start();
require_once("definitions.php");

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

$store_key = isset($_SESSION['storekey']) ? intval($_SESSION['storekey']) : 0;
if ($store_key === 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid store key']);
    exit;
}

// Get POST data and sanitize/validate
$package_size_key = isset($_POST['package_size_key']) ? intval($_POST['package_size_key']) : 0;
$item_type = 1; //  1 = package, 2 = mail

// Required fields from form
$name = trim($_POST['name'] ?? '');
$price_group = isset($_POST['price_group']) ? intval($_POST['price_group']) : 0;
$order = isset($_POST['order']) ? intval($_POST['order']) : 0;
$price_initial = isset($_POST['price_initial']) ? floatval($_POST['price_initial']) : 0.0;
$price_per_extra_days = isset($_POST['price_per_extra_days']) ? floatval($_POST['price_per_extra_days']) : 0.0;
$extra_days = isset($_POST['extra_days']) ? intval($_POST['extra_days']) : 0;
$price_per_extra_months = isset($_POST['price_per_extra_months']) ? floatval($_POST['price_per_extra_months']) : 0.0;
$price_max = isset($_POST['price_max']) ? floatval($_POST['price_max']) : 0.0;
$active = isset($_POST['active']) ? intval($_POST['active']) : 0;

// Basic validation
if ($name === '') {
    echo json_encode(['success' => false, 'message' => 'Name is required']);
    exit;
}

if ($price_group === 0) {
    echo json_encode(['success' => false, 'message' => 'Price Group is required']);
    exit;
}

$conn = new mysqli(PACKAGE_LOG_DB_SERVER, PACKAGE_LOG_DB_USER, PACKAGE_LOG_DB_PASS, PACKAGE_LOG_DB_NAME);
if ($conn->connect_error) {
    echo json_encode(['success' => false, 'message' => 'Database connection failed']);
    exit;
}

if ($package_size_key > 0) {
    // Update existing record
    $sql = "UPDATE mailbox_item_sizes SET 
        store_key = ?,
        name = ?,
        price_group = ?,
        `order` = ?,
        price_initial = ?,
        price_per_extra_days = ?,
        extra_days = ?,
        price_per_extra_months = ?,
        price_max = ?,
        active = ?
        WHERE item_size_key = ? AND store_key = ?";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        echo json_encode(['success' => false, 'message' => 'Prepare failed: ' . $conn->error]);
        $conn->close();
        exit;
    }

    $stmt->bind_param(
        "isiiddiddiii",
        $store_key,
        $name,
        $price_group,
        $order,
        $price_initial,
        $price_per_extra_days,
        $extra_days,
        $price_per_extra_months,
        $price_max,
        $active,
        $package_size_key,
        $store_key
    );

    if ($stmt->execute()) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Execute failed: ' . $stmt->error]);
    }
    $stmt->close();
} else {
    // Insert new record
    $sql = "INSERT INTO mailbox_item_sizes (
        store_key, name, item_type, price_group, `order`, price_initial, price_per_extra_days, extra_days,
        price_per_extra_months, price_max, active
    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        echo json_encode(['success' => false, 'message' => 'Prepare failed: ' . $conn->error]);
        $conn->close();
        exit;
    }

    $stmt->bind_param(
        "isiiiddiddi",
        $store_key,
        $name,
        $item_type,
        $price_group,
        $order,
        $price_initial,
        $price_per_extra_days,
        $extra_days,
        $price_per_extra_months,
        $price_max,
        $active
    );

    if ($stmt->execute()) {
        echo json_encode(['success' => true, 'package_size_key' => $stmt->insert_id]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Execute failed: ' . $stmt->error]);
    }
    $stmt->close();
}

$conn->close();