<?php
/**
 * FreshCloud Mail — resume a draft
 * GET /api/email/compose/{id}?user_id=...
 */

require_once __DIR__ . '/../_bootstrap.php';

$draftId = basename(ltrim($_SERVER['PATH_INFO'] ?? $_GET['draft_id'] ?? '', '/'));
$userId = $_GET['user_id'] ?? null;

if (!$draftId || !$userId) {
    freshcloud_mail_error(400, 'missing_params', 'draft_id (path) and user_id (query) are required');
}

$composer = freshcloud_mail_composer($userId, null);
if ($composer !== null) {
    try {
        $request = $composer->resumeDraft($draftId, $userId);
        freshcloud_mail_json(200, [
            'status' => 'draft_resumed',
            'draft' => $request->toArray(),
            'timestamp' => gmdate('c'),
        ]);
    } catch (\Throwable $e) {
        freshcloud_mail_error(500, 'composer_failed', $e->getMessage());
    }
}

freshcloud_mail_json(200, [
    'status' => 'draft_resumed',
    'draft' => [
        'id' => $draftId,
        'user_id' => $userId,
        'mode' => 'new',
        'to' => [], 'cc' => [], 'bcc' => [],
        'subject' => '', 'body_html' => '', 'body_text' => '',
        'attachment_ids' => [],
        'status' => 'draft',
    ],
    'note' => 'v0.1.0-test stub — Composer::resumeDraft not yet wired',
    'timestamp' => gmdate('c'),
]);
