<?php
namespace Misuzu\Users;

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

class BanInfo {
    private string $id;
    private string $userId;
    private ?string $modId;
    private int $severity;
    private string $publicReason;
    private string $privateReason;
    private int $created;
    private ?int $expires;

    public function __construct(IDbResult $result) {
        $this->id = (string)$result->getInteger(0);
        $this->userId = (string)$result->getInteger(1);
        $this->modId = $result->isNull(2) ? null : (string)$result->getInteger(2);
        $this->severity = $result->getInteger(3);
        $this->publicReason = $result->getString(4);
        $this->privateReason = $result->getString(5);
        $this->created = $result->getInteger(6);
        $this->expires = $result->isNull(7) ? null : $result->getInteger(7);
    }

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

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

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

    public function getModId(): ?string {
        return $this->modId;
    }

    public function getSeverity(): int {
        return $this->severity;
    }

    public function hasPublicReason(): bool {
        return $this->publicReason !== '';
    }

    public function getPublicReason(): string {
        return $this->publicReason;
    }

    public function hasPrivateReason(): bool {
        return $this->privateReason !== '';
    }

    public function getPrivateReason(): string {
        return $this->privateReason;
    }

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

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

    public function isPermanent(): bool {
        return $this->expires === null;
    }

    public function getExpiresTime(): ?int {
        return $this->expires;
    }

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

    public function isActive(): bool {
        return $this->expires === null || $this->expires > time();
    }

    public function isExpired(): bool {
        return $this->expires !== null && $this->expires <= time();
    }

    private const DURATION_DIVS = [
        31536000 => 'year',
         2592000 => 'month',
          604800 => 'week',
           86400 => 'day',
            3600 => 'hour',
              60 => 'minute',
               1 => 'second',
    ];

    private static function getTimeString(?int $left, int $right): string {
        if($left === null)
            return 'permanent';

        $duration = $left - $right;
        foreach(self::DURATION_DIVS as $span => $name) {
            $display = floor($duration / $span);
            if($display > 0)
                return number_format($display) . ' ' . $name . ($display == 1 ? '' : 's');
        }

        return 'an amount of time';
    }

    public function getDurationString(): string {
        return self::getTimeString($this->expires, $this->created);
    }

    public function getRemainingString(): string {
        return self::getTimeString($this->expires, time());
    }
}
