<?php

require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../config/helpers.php';
require_once __DIR__ . '/../../../middleware/admin.php';
require_once __DIR__ . '/MailboxRepository.php';

requireAdmin();

if (getMethod() !== 'GET') {
    jsonResponse(false, 'method.not_allowed', null, 405);
}

$route = getApiRouteSegments();
$mailboxId = isset($route[2]) && ctype_digit((string) $route[2]) ? (int) $route[2] : 0;
$messageId = isset($route[4]) && ctype_digit((string) $route[4]) ? (int) $route[4] : 0;

global $pdo;
$repo = new MailboxRepository($pdo);

if ($mailboxId <= 0 || !$repo->findById($mailboxId)) {
    jsonResponse(false, 'mailboxes.not_found', ['route' => $route], 404);
}

$message = $repo->getMessageById($mailboxId, $messageId);
if (!$message) {
    jsonResponse(false, 'mailboxes.message_not_found', ['mailbox_id' => $mailboxId, 'message_id' => $messageId], 404);
}

$attachments = $repo->getMessageAttachmentsByMessageId($messageId);
$preparedHtml = replaceInlineCidSources($message['html_body'] ?? null, $attachments, $mailboxId, $messageId);

$repo->markMessageAsRead($mailboxId, $messageId);

$message['html_body'] = $preparedHtml;
$message['attachments'] = array_map(
    static function (array $attachment) {
        $attachment['download_url'] = (string) ($attachment['public_url'] ?: $attachment['storage_path']);
        return $attachment;
    },
    $attachments
);

jsonResponse(true, 'mailboxes.message_show_success', $message);

// require_once __DIR__ . '/../../../config/database.php';
// require_once __DIR__ . '/../../../config/helpers.php';
// require_once __DIR__ . '/../../../middleware/admin.php';
// require_once __DIR__ . '/MailboxRepository.php';

// requireAdmin();

// if (getMethod() !== 'GET') {
//     jsonResponse(false, 'method.not_allowed', null, 405);
// }

// $route = getApiRouteSegments();
// $mailboxId = isset($route[2]) && ctype_digit((string) $route[2]) ? (int) $route[2] : 0;
// $messageId = isset($route[4]) && ctype_digit((string) $route[4]) ? (int) $route[4] : 0;

// global $pdo;
// $repo = new MailboxRepository($pdo);

// if ($mailboxId <= 0 || !$repo->findById($mailboxId)) {
//     jsonResponse(false, 'mailboxes.not_found', ['route' => $route], 404);
// }

// $message = $repo->getMessageById($mailboxId, $messageId);
// if (!$message) {
//     jsonResponse(false, 'mailboxes.message_not_found', ['mailbox_id' => $mailboxId, 'message_id' => $messageId], 404);
// }

// $attachments = $repo->getMessageAttachmentsByMessageId($messageId);
// $preparedHtml = replaceInlineCidSources($message['html_body'] ?? null, $attachments, $mailboxId, $messageId);

// $repo->markMessageAsRead($mailboxId, $messageId);

// $message['html_body'] = $preparedHtml;
// $message['attachments'] = array_map(
//     static function (array $attachment) use ($mailboxId, $messageId) {
//         $attachment['download_url'] = '/v1/api/Admin/mailboxes/' . $mailboxId . '/messages/' . $messageId . '/download/' . (int) $attachment['id'];
//         $attachment['inline_url'] = '/v1/api/Admin/mailboxes/' . $mailboxId . '/messages/' . $messageId . '/inline/' . (int) $attachment['id'];
//         return $attachment;
//     },
//     $attachments
// );

// jsonResponse(true, 'mailboxes.message_show_success', $message);