<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
header('Content-Type: application/json');

require_once("definitions.php");

// Validate mailbox_key parameter
if (!isset($_GET['mailbox_key']) || !is_numeric($_GET['mailbox_key'])) {
    echo json_encode(['success' => false, 'message' => 'Invalid mailbox key']);
    exit;
}

$mailbox_key = intval($_GET['mailbox_key']);

// Get store_key from session
$store_key = $_SESSION['storekey'] ?? 0;
if ($store_key <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid store key in session']);
    exit;
}

// Create DB connection
$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;
}

// Fetch mailbox details
$stmt = $conn->prepare("
    SELECT mailbox_key, store_key, mailboxnumber, mailbox_attributes_key, mailbox_name 
    FROM mailboxes 
    WHERE mailbox_key = ? AND store_key = ?
");
$stmt->bind_param("ii", $mailbox_key, $store_key);
$stmt->execute();
$mailboxResult = $stmt->get_result();

if ($mailboxResult->num_rows === 0) {
    echo json_encode(['success' => false, 'message' => 'Mailbox not found']);
    $stmt->close();
    $conn->close();
    exit;
}

$mailbox = $mailboxResult->fetch_assoc();
$stmt->close();

// Fetch mailbox attributes based on mailbox_attributes_key and store_key
$attrStmt = $conn->prepare("
    SELECT mailbox_attributes_key, store_key, name, length, width, virtualbox, groupbox, ps1583, webviewable, businesses
    FROM mailbox_attributes
    WHERE mailbox_attributes_key = ? AND store_key = ?
");
$attrStmt->bind_param("ii", $mailbox['mailbox_attributes_key'], $store_key);
$attrStmt->execute();
$attrResult = $attrStmt->get_result();

$attributes = [];
if ($attrResult) {
    while ($row = $attrResult->fetch_assoc()) {
        $attributes[] = $row;
    }
}
$attrStmt->close();

// Fetch all mailbox attribute names for current store_key to populate the dropdown
$namesStmt = $conn->prepare("
    SELECT DISTINCT name 
    FROM mailbox_attributes 
    WHERE store_key = ? 
    ORDER BY name ASC
");
$namesStmt->bind_param("i", $store_key);
$namesStmt->execute();
$namesResult = $namesStmt->get_result();

$attributeNames = [];
if ($namesResult) {
    while ($row = $namesResult->fetch_assoc()) {
        $attributeNames[] = $row['name'];
    }
}
$namesStmt->close();

// Fetch plans for the mailbox attribute associated with the mailbox and store_key
$plansStmt = $conn->prepare("
    SELECT plan_key, store_key, mailbox_attribute_key, package_size_price_group_key, name, `order`, pkglimit_mo, one_mo, three_mo, six_mo, twelve_mo, webviewable, webrentable, businesses
    FROM mailbox_plans 
    WHERE mailbox_attribute_key = ? AND store_key = ?
    ORDER BY name ASC
");
$plansStmt->bind_param("ii", $mailbox['mailbox_attributes_key'], $store_key);
$plansStmt->execute();
$plansResult = $plansStmt->get_result();

$plans = [];
if ($plansResult) {
    while ($row = $plansResult->fetch_assoc()) {
        $plans[] = $row;
    }
}
$plansStmt->close();

$conn->close();

echo json_encode([
    'success' => true,
    'data' => [
        'mailbox' => $mailbox,
        'attributes' => $attributes,
        'plans' => $plans,
        'attribute_names' => $attributeNames
    ]
]);