<?php

declare(strict_types=1);

namespace PhpWA\Client;

use InvalidArgumentException;
use PhpWA\Protocol\Jid\Jid;
use PhpWA\Protocol\Proto\Message;

final class StoredOutgoingMessage
{
    /** @var string */ private $id;
    /** @var string */ private $to;
    /** @var bool */ private $group;
    /** @var Message */ private $message;
    /** @var int */ private $createdAt;

    public function __construct(string $id, string $to, bool $group, Message $message, int $createdAt)
    {
        $jid = Jid::parse($to); $bytes = $message->serializeToString();
        if ($id === '' || strlen($id) > 256 || preg_match('/[\x00-\x1f\x7f]/', $id) === 1
            || !$jid instanceof Jid || $jid->isGroup() !== $group || $bytes === '' || strlen($bytes) > 3000000 || $createdAt < 0) {
            throw new InvalidArgumentException('Stored outgoing message is invalid.');
        }
        $this->id = $id; $this->to = (string) $jid; $this->group = $group; $this->message = $message; $this->createdAt = $createdAt;
    }

    public function id(): string { return $this->id; }
    public function to(): string { return $this->to; }
    public function isGroup(): bool { return $this->group; }
    public function message(): Message { return $this->message; }
    public function createdAt(): int { return $this->createdAt; }
}
