<?php

declare(strict_types=1);

class ContactMessage
{
    public static function all(int $limit = 20, int $offset = 0, bool $unreadOnly = false): array
    {
        $where = $unreadOnly ? 'WHERE is_read = 0' : '';
        return Database::fetchAll(
            "SELECT * FROM contact_messages {$where} ORDER BY created_at DESC LIMIT ? OFFSET ?",
            [$limit, $offset]
        );
    }

    public static function find(int $id): ?array
    {
        return Database::fetchOne('SELECT * FROM contact_messages WHERE id = ?', [$id]);
    }

    public static function count(bool $unreadOnly = false): int
    {
        return $unreadOnly
            ? Database::count('contact_messages', 'is_read = 0')
            : Database::count('contact_messages');
    }

    public static function countUnread(): int
    {
        return Database::count('contact_messages', 'is_read = 0');
    }

    public static function create(array $data): int
    {
        return Database::insert('contact_messages', $data);
    }

    public static function markRead(int $id): void
    {
        Database::update('contact_messages', ['is_read' => 1], 'id = ?', [$id]);
    }

    public static function delete(int $id): void
    {
        Database::delete('contact_messages', 'id = ?', [$id]);
    }
}
