<?php
namespace Misuzu\News;

use Index\DateTime;
use Index\Data\IDbResult;

class NewsPostInfo {
    private string $id;
    private string $categoryId;
    private ?string $userId;
    private ?string $commentsSectionId;
    private bool $featured;
    private string $title;
    private string $body;
    private int $scheduled;
    private int $created;
    private int $updated;
    private ?int $deleted;

    public function __construct(IDbResult $result) {
        $this->id = (string)$result->getInteger(0);
        $this->categoryId = (string)$result->getInteger(1);
        $this->userId = $result->isNull(2) ? null : (string)$result->getInteger(2);
        $this->commentsSectionId = $result->isNull(3) ? null : (string)$result->getInteger(3);
        $this->featured = $result->getInteger(4) !== 0;
        $this->title = $result->getString(5);
        $this->body = $result->getString(6);
        $this->scheduled = $result->getInteger(7);
        $this->created = $result->getInteger(8);
        $this->updated = $result->getInteger(9);
        $this->deleted = $result->isNull(10) ? null : $result->getInteger(10);
    }

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

    public function getCategoryId(): string {
        return $this->categoryId;
    }

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

    public function getUserId(): ?string {
        return $this->userId;
    }

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

    public function getCommentsCategoryId(): string {
        return $this->commentsSectionId;
    }

    public function getCommentsCategoryName(): string {
        return sprintf('news-%s', $this->id);
    }

    public function isFeatured(): bool {
        return $this->featured;
    }

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

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

    public function getFirstParagraph(): string {
        $index = mb_strpos($this->body, "\n");
        return $index === false ? $this->body : mb_substr($this->body, 0, $index);
    }

    public function getScheduledTime(): int {
        return $this->scheduled;
    }

    public function getScheduledAt(): DateTime {
        return DateTime::fromUnixTimeSeconds($this->scheduled);
    }

    public function isPublished(): bool {
        return $this->scheduled <= time();
    }

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

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

    public function getUpdatedTime(): int {
        return $this->updated;
    }

    public function getUpdatedAt(): DateTime {
        return DateTime::fromUnixTimeSeconds($this->updated);
    }

    public function isEdited(): bool {
        return $this->updated > $this->created;
    }

    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);
    }
}
