<?php
declare(strict_types=1);

/**
 * PATCH /api/email/messages/{id}/move
 * Move a message to another folder. Body: {folder: 'Trash' | 'Archive' | 'INBOX'}.
 */

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

require_once __DIR__ . '/_bootstrap.php';

use Oscar\MailEngine\ImapClient\InMemoryImapClient;

try {
    $input = json_decode(file_get_contents('php://input'), true) ?? [];
    if (!isset($input['folder'])) {
        throw new \InvalidArgumentException('Target folder is required');
    }
    $messageId = $_SERVER['FRESH_ROUTE_PARAMS']['id'] ?? null;
    if (!$messageId) {
        throw new \InvalidArgumentException('Message ID is required');
    }
    $accountId = 'acc1';
    $imap = new InMemoryImapClient();
    $imap->move($messageId, $input['folder'], $accountId);
    echo json_encode(['ok' => true, 'new_folder' => $input['folder']]);
} catch (\Throwable $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
