<?php
declare(strict_types=1);

/**
 * GET /api/email/messages/{id}/body
 * Returns full message body. v0.1.0: hardcoded demo body.
 * v0.2.0: real MIME parsing.
 */

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

require_once __DIR__ . '/_bootstrap.php';

try {
    $messageId = $_SERVER['FRESH_ROUTE_PARAMS']['id'] ?? null;
    if (!$messageId) {
        throw new \InvalidArgumentException('Message ID is required');
    }
    // v0.1.0: hardcoded demo body. v0.2.0: real MIME parsing.
    echo json_encode([
        'id' => $messageId,
        'html' => '<p>Hello from FreshMail!</p><p>This is a demo message body for v0.1.0.</p><p>The real MIME parser is coming in v0.2.0.</p>',
        'text' => "Hello from FreshMail!\n\nThis is a demo message body for v0.1.0.\nThe real MIME parser is coming in v0.2.0.",
        'headers' => [
            'from' => 'sender@example.com',
            'to' => 'mark@treeschedulers.uk',
            'cc' => '',
            'bcc' => '',
            'subject' => 'Welcome to FreshMail',
            'date' => gmdate('c'),
        ],
        'attachments' => [],
    ], JSON_PRETTY_PRINT);
} catch (\Throwable $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
