<?php
declare(strict_types=1);

namespace FreshCloud\Mail\Compose;

final readonly class ComposeRequest
{
    public function __construct(
        public string $mode,
        public array $to,
        public array $cc,
        public array $bcc,
        public string $subject,
        public string $bodyHtml,
        public string $bodyText,
        public array $attachmentIds,
        public string $accountId,
        public ?string $draftId,
        public bool $saveAsDraft,
        public ?string $replyToMsgId,
        public ?string $forwardOfMsgId
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            mode: $data['mode'],
            to: $data['to'] ?? [],
            cc: $data['cc'] ?? [],
            bcc: $data['bcc'] ?? [],
            subject: $data['subject'] ?? '',
            bodyHtml: $data['body_html'] ?? '',
            bodyText: $data['body_text'] ?? '',
            attachmentIds: $data['attachment_ids'] ?? [],
            accountId: $data['account_id'] ?? '',
            draftId: $data['draft_id'] ?? null,
            saveAsDraft: $data['save_as_draft'] ?? false,
            replyToMsgId: $data['reply_to_msg_id'] ?? null,
            forwardOfMsgId: $data['forward_of_msg_id'] ?? null
        );
    }

    public function toArray(): array
    {
        return [
            'mode' => $this->mode,
            'to' => $this->to,
            'cc' => $this->cc,
            'bcc' => $this->bcc,
            'subject' => $this->subject,
            'body_html' => $this->bodyHtml,
            'body_text' => $this->bodyText,
            'attachment_ids' => $this->attachmentIds,
            'account_id' => $this->accountId,
            'draft_id' => $this->draftId,
            'save_as_draft' => $this->saveAsDraft,
            'reply_to_msg_id' => $this->replyToMsgId,
            'forward_of_msg_id' => $this->forwardOfMsgId
        ];
    }
}
