<?php

declare(strict_types=1);

namespace PhpWA\Client;

use InvalidArgumentException;

final class SessionEvent
{
    public const QR = 'auth.qr';
    public const PAIRING_SUCCESS = 'auth.pairing_success';
    public const PAIRING_CODE_CONFIRMED = 'auth.pairing_code_confirmed';
    public const ONLINE = 'connection.online';
    public const DISCONNECTED = 'connection.disconnected';
    public const OFFLINE_PREVIEW = 'sync.offline_preview';
    public const OFFLINE_COMPLETE = 'sync.offline_complete';
    public const ROUTING_UPDATED = 'connection.routing_updated';
    public const IGNORED = 'protocol.ignored';

    /** @var string */ private $type;
    /** @var array<string,mixed> */ private $data;
    /** @var bool */ private $reconnect;
    /** @var bool */ private $credentialResetRequired;

    /** @param array<string,mixed> $data */
    public function __construct(string $type, array $data = [], bool $reconnect = false, bool $credentialResetRequired = false)
    {
        if (preg_match('/^[a-z][a-z0-9_.]{0,63}$/D', $type) !== 1) {
            throw new InvalidArgumentException('Session event type is invalid.');
        }
        $this->type = $type;
        $this->data = $data;
        $this->reconnect = $reconnect;
        $this->credentialResetRequired = $credentialResetRequired;
    }

    public function type(): string { return $this->type; }
    /** @return array<string,mixed> */ public function data(): array { return $this->data; }
    public function shouldReconnect(): bool { return $this->reconnect; }
    public function credentialResetRequired(): bool { return $this->credentialResetRequired; }
}
