<?php
namespace Misuzu\Storage\Files;

use Carbon\CarbonImmutable;
use Index\UriBase64;
use Index\Db\DbResult;

class FileInfo {
    public function __construct(
        public private(set) string $id,
        public private(set) string $hash,
        public private(set) string $type,
        public private(set) int $size,
        public private(set) int $createdTime,
    ) {}

    public static function fromResult(DbResult $result): FileInfo {
        return new FileInfo(
            id: $result->getString(0),
            hash: $result->getString(1),
            type: $result->getString(2),
            size: $result->getInteger(3),
            createdTime: $result->getInteger(4),
        );
    }

    public string $hashString {
        get => UriBase64::encode($this->hash);
    }

    public string $hashHexString {
        get => bin2hex($this->hash);
    }

    public CarbonImmutable $createdAt {
        get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
    }

    public bool $isImage {
        get => str_starts_with($this->type, 'image/');
    }

    public bool $isVideo {
        get => str_starts_with($this->type, 'video/');
    }

    public bool $isAudio {
        get => str_starts_with($this->type, 'audio/');
    }
}