<?php
namespace Misuzu\Changelog;

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

class ChangeInfo {
    private string $id;
    private ?string $userId;
    private int $action;
    private int $created;
    private string $summary;
    private string $body;

    public function __construct(IDbResult $result) {
        $this->id = (string)$result->getInteger(0);
        $this->userId = $result->isNull(1) ? null : (string)$result->getInteger(1);
        $this->action = $result->getInteger(2);
        $this->created = $result->getInteger(3);
        $this->summary = $result->getString(4);
        $this->body = $result->getString(5);
    }

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

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

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

    public function getActionId(): int {
        return $this->action;
    }

    public function getAction(): string {
        return Changelog::convertFromActionId($this->action);
    }

    public function getActionText(): string {
        return Changelog::actionText($this->action);
    }

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

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

    public function getDate(): string {
        return gmdate('Y-m-d', $this->created);
    }

    public function getSummary(): string {
        return $this->summary;
    }

    public function hasBody(): bool {
        return !empty($this->body);
    }

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

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