<?php
namespace Misuzu\Messages;

use Index\DateTime;
use Index\Data\IDbResult;
use Misuzu\Parsers\Parser;

class MessageInfo {
    public function __construct(
        private string $messageId,
        private string $ownerId,
        private ?string $authorId,
        private ?string $recipientId,
        private ?string $replyTo,
        private string $title,
        private string $body,
        private int $parser,
        private int $created,
        private ?int $sent,
        private ?int $read,
        private ?int $deleted,
    ) {}

    public static function fromResult(IDbResult $result): MessageInfo {
        return new MessageInfo(
            messageId: $result->getString(0),
            ownerId: $result->getString(1),
            authorId: $result->getStringOrNull(2),
            recipientId: $result->getStringOrNull(3),
            replyTo: $result->getStringOrNull(4),
            title: $result->getString(5),
            body: $result->getString(6),
            parser: $result->getInteger(7),
            created: $result->getInteger(8),
            sent: $result->getIntegerOrNull(9),
            read: $result->getIntegerOrNull(10),
            deleted: $result->getIntegerOrNull(11),
        );
    }

    public function getId(): string {
        return $this->messageId;
    }

    public function getOwnerId(): string {
        return $this->ownerId;
    }

    public function hasAuthorId(): bool {
        return $this->authorId !== null;
    }

    public function getAuthorId(): ?string {
        return $this->authorId;
    }

    public function hasRecipientId(): bool {
        return $this->recipientId !== null;
    }

    public function getRecipientId(): ?string {
        return $this->recipientId;
    }

    public function hasReplyToId(): bool {
        return $this->replyTo !== null;
    }

    public function getReplyToId(): ?string {
        return $this->replyTo;
    }

    public function getTitle(): string {
        return $this->title;
    }

    public function getBody(): string {
        return $this->body;
    }

    public function getParser(): int {
        return $this->parser;
    }

    public function isBodyPlain(): bool {
        return $this->parser === Parser::PLAIN;
    }

    public function isBodyBBCode(): bool {
        return $this->parser === Parser::BBCODE;
    }

    public function isBodyMarkdown(): bool {
        return $this->parser === Parser::MARKDOWN;
    }

    public function getCreatedTime(): int {
        return $this->created;
    }

    public function getCreatedAt(): DateTime {
        return DateTime::fromUnixTimeSeconds($this->created);
    }

    public function isSent(): bool {
        return $this->sent !== null;
    }

    public function getSentTime(): ?int {
        return $this->sent;
    }

    public function getSentAt(): ?DateTime {
        return $this->sent === null ? null : DateTime::fromUnixTimeSeconds($this->sent);
    }

    public function isRead(): bool {
        return $this->read !== null;
    }

    public function getReadTime(): ?int {
        return $this->read;
    }

    public function getReadAt(): ?DateTime {
        return $this->read === null ? null : DateTime::fromUnixTimeSeconds($this->read);
    }

    public function isDeleted(): bool {
        return $this->deleted !== null;
    }

    public function getDeletedTime(): ?int {
        return $this->deleted;
    }

    public function getDeletedAt(): ?DateTime {
        return $this->deleted === null ? null : DateTime::fromUnixTimeSeconds($this->deleted);
    }

    public function getDisplayTime(): int {
        if($this->isSent())
            return $this->getSentTime();
        return $this->getCreatedTime();
    }

    public function getDisplayAt(): DateTime {
        if($this->isSent())
            return $this->getSentAt();
        return $this->getCreatedAt();
    }
}
