<?php
require_once __DIR__ . '/DB.php';

class User {
    public int $id;
    public string $username;
    public bool $is_banned;
    public ?string $ban_until;

    public static function fromSession(): ?User {
        if (!isset($_SESSION['user_id'])) return null;
        return self::findById((int)$_SESSION['user_id']);
    }

    public static function findById(int $id): ?User {
        $db = DB::get();
        $stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
        $stmt->execute([$id]);
        $row = $stmt->fetch();
        if (!$row) return null;

        $u = new self();
        $u->id = (int)$row['id'];
        $u->username = $row['username'];
        $u->is_banned = (bool)$row['is_banned'];
        $u->ban_until = $row['ban_until'];
        return $u;
    }

    public static function isBanned(User $u): bool {
        if (!$u->is_banned) return false;
        if ($u->ban_until === null) return true; // perm
        return (strtotime($u->ban_until) > time());
    }
}