<?php
session_start();
require_once("definitions.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


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

// Get store_key from session or set default (adjust as needed)
$store_key = isset($_SESSION['storekey']) ? intval($_SESSION['storekey']) : 1;

// Helper to get POST value or default
function get_post_val($name, $default = null) {
    return isset($_POST[$name]) ? trim($_POST[$name]) : $default;
}

// Prepare all variables from form inputs
$mailbox_attribute_key = intval(get_post_val('mailbox_type', 0));
$name = get_post_val('plan_name', '');
$order = 0;  // no form input for order, default 0

$customer_limit = intval(get_post_val('occupant_limit', 0));
$customer_hardlimit = intval(get_post_val('hard_limit', 0));
$pkglimit_mo = intval(get_post_val('monthly_package_limit', 0));
$pkglimit_insta = intval(get_post_val('instant_package_limit', 0));

$one_mo = floatval(get_post_val('price_1mo', 0));
$one_mo_free = intval(get_post_val('free_months_1mo', 0));
$three_mo = floatval(get_post_val('price_3mo', 0));
$three_mo_free = intval(get_post_val('free_months_3mo', 0));
$six_mo = floatval(get_post_val('price_6mo', 0));
$six_mo_free = intval(get_post_val('free_months_6mo', 0));
$twelve_mo = floatval(get_post_val('price_12mo', 0));
$twelve_mo_free = intval(get_post_val('free_months_12mo', 0));

$webviewable = isset($_POST['web_viewable']) ? 1 : 0;

// Fees not in form, default 0.00
$docfee = 0.00;
$startupfee = 0.00;
$keydepo = 0.00;

// Flags default 0
$webrentable = 0;
$businesses = 0;

// New columns added per your earlier request (allowances and fees)
$incoming_mail_items_allowance = intval(get_post_val('incoming_mail_items_allowance', 0));
$incoming_mail_items_extra_fee = floatval(get_post_val('incoming_mail_items_extra_fee', 0));

$open_scan_requests_allowance = intval(get_post_val('open_scan_requests_allowance', 0));
$open_scan_requests_extra_fee = floatval(get_post_val('open_scan_requests_extra_fee', 0));

$scanned_pages_per_month_allowance = intval(get_post_val('scanned_pages_per_month_allowance', 0));
$scanned_pages_per_month_extra_fee = floatval(get_post_val('scanned_pages_per_month_extra_fee', 0));

$scanned_pages_per_request_allowance = intval(get_post_val('scanned_pages_per_request_allowance', 0));
$scanned_pages_per_request_extra_fee = floatval(get_post_val('scanned_pages_per_request_extra_fee', 0));

$forward_requests_allowance = intval(get_post_val('forward_requests_allowance', 0));
$forward_requests_extra_fee = floatval(get_post_val('forward_requests_extra_fee', 0));

$check_deposit_requests_allowance = intval(get_post_val('check_deposit_requests_allowance', 0));
$check_deposit_requests_extra_fee = floatval(get_post_val('check_deposit_requests_extra_fee', 0));

$recycle_requests_allowance = intval(get_post_val('recycle_requests_allowance', 0));
$recycle_requests_extra_fee = floatval(get_post_val('recycle_requests_extra_fee', 0));

$shred_requests_allowance = intval(get_post_val('shred_requests_allowance', 0));
$shred_requests_extra_fee = floatval(get_post_val('shred_requests_extra_fee', 0));

$visit_limit_per_month = intval(get_post_val('visit_limit_per_month', 0));
$cost_per_visit = floatval(get_post_val('cost_per_visit', 0));
$local_pickup = isset($_POST['local_pickup']) ? 1 : 0;

// Basic validation
if ($name === '' || $mailbox_attribute_key === 0) {
    echo json_encode(['success' => false, 'message' => 'Plan name and mailbox type are required.']);
    exit;
}

// Prepare and bind
$stmt = $conn->prepare("INSERT INTO mailbox_plans (
    store_key, mailbox_attribute_key, `name`, `order`, customer_limit, customer_hardlimit,
    pkglimit_mo, pkglimit_insta, one_mo, one_mo_free, three_mo, three_mo_free, six_mo, six_mo_free,
    twelve_mo, twelve_mo_free, docfee, startupfee, keydepo, webviewable, webrentable, businesses,
    incoming_mail_items_allowance, incoming_mail_items_extra_fee,
    open_scan_requests_allowance, open_scan_requests_extra_fee,
    scanned_pages_per_month_allowance, scanned_pages_per_month_extra_fee,
    scanned_pages_per_request_allowance, scanned_pages_per_request_extra_fee,
    forward_requests_allowance, forward_requests_extra_fee,
    check_deposit_requests_allowance, check_deposit_requests_extra_fee,
    recycle_requests_allowance, recycle_requests_extra_fee,
    shred_requests_allowance, shred_requests_extra_fee,
    visit_limit_per_month, cost_per_visit, local_pickup
) VALUES (
    ?, ?, ?, ?, ?, ?,
    ?, ?, ?, ?, ?, ?, ?, ?,
    ?, ?, ?, ?, ?, ?, ?, ?,
    ?, ?,
    ?, ?,
    ?, ?,
    ?, ?,
    ?, ?,
    ?, ?,
    ?, ?,
    ?, ?,
    ?, ?, ?
)");

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

$types = "iiisiiiiiiiiiiiiiiiiiiidididididididididi";

$stmt->bind_param($types,
    $store_key,
    $mailbox_attribute_key,
    $name,
    $order,
    $customer_limit,
    $customer_hardlimit,
    $pkglimit_mo,
    $pkglimit_insta,
    $one_mo,
    $one_mo_free,
    $three_mo,
    $three_mo_free,
    $six_mo,
    $six_mo_free,
    $twelve_mo,
    $twelve_mo_free,
    $docfee,
    $startupfee,
    $keydepo,
    $webviewable,
    $webrentable,
    $businesses,
    $incoming_mail_items_allowance,
    $incoming_mail_items_extra_fee,
    $open_scan_requests_allowance,
    $open_scan_requests_extra_fee,
    $scanned_pages_per_month_allowance,
    $scanned_pages_per_month_extra_fee,
    $scanned_pages_per_request_allowance,
    $scanned_pages_per_request_extra_fee,
    $forward_requests_allowance,
    $forward_requests_extra_fee,
    $check_deposit_requests_allowance,
    $check_deposit_requests_extra_fee,
    $recycle_requests_allowance,
    $recycle_requests_extra_fee,
    $shred_requests_allowance,
    $shred_requests_extra_fee,
    $visit_limit_per_month,
    $cost_per_visit,
    $local_pickup
);

if ($stmt->execute()) {
    echo json_encode(['success' => true, 'message' => 'New mailbox plan added successfully.']);
} else {
    echo json_encode(['success' => false, 'message' => 'Insert failed: ' . $stmt->error]);
}

$stmt->close();
$conn->close(); 