<?php
namespace Misuzu\Auth;

use Index\DateTime;
use Index\Data\IDbResult;
use Index\Net\IPAddress;
use Misuzu\ClientInfo;

class LoginAttemptInfo {
    private ?string $userId;
    private bool $success;
    private string $remoteAddr;
    private string $countryCode;
    private int $created;
    private string $userAgent;
    private string $clientInfo;

    public function __construct(IDbResult $result) {
        $this->userId = $result->isNull(0) ? null : (string)$result->getInteger(0);
        $this->success = $result->getInteger(1) !== 0;
        $this->remoteAddr = $result->getString(2);
        $this->countryCode = $result->getString(3);
        $this->created = $result->getInteger(4);
        $this->userAgent = $result->getString(5);
        $this->clientInfo = $result->getString(6);
    }

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

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

    public function isSuccess(): bool {
        return $this->success;
    }

    public function getRemoteAddressRaw(): string {
        return $this->remoteAddr;
    }

    public function getRemoteAddress(): IPAddress {
        return IPAddress::parse($this->remoteAddr);
    }

    public function getCountryCode(): string {
        return $this->countryCode;
    }

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

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

    public function getUserAgentString(): string {
        return $this->userAgent;
    }

    public function getClientInfoRaw(): string {
        return $this->clientInfo;
    }

    public function getClientInfo(): ClientInfo {
        return ClientInfo::decode($this->clientInfo);
    }
}
