<?php

declare(strict_types=1);

namespace Atdt;

/**
 * What a door's console admin panel receives (SPEC §13.6): the store, a
 * logger, and nothing else — the wrapper page handles auth and CSRF.
 *
 * 1.5.5: panels may also post an announcement message as the sysop (a
 * round opening), through the same mediated path as door cron hooks.
 */
final class DoorAdminContext
{
    public function __construct(
        private readonly DoorStore $store,
        private readonly string $slug,
        private readonly ?Db $db = null,
        private readonly ?Settings $settings = null,
    ) {
    }

    public function kv(): DoorStore
    {
        return $this->store;
    }

    public function table(string $name): DoorTable
    {
        return $this->store->table($name);
    }

    public function tx(callable $fn): mixed
    {
        return $this->store->tx($fn);
    }

    public function log(string $msg): void
    {
        atdt_log_event('admin', 'door:' . $this->slug, mb_strimwidth($msg, 0, 200));
    }

    /**
     * Post a message into a board area as the sysop (announcements).
     * @param list<string> $lines pipe-coded body lines
     */
    public function postMessage(string $areaTag, string $subject, array $lines): bool
    {
        if ($this->db === null || $this->settings === null) {
            return false;
        }
        return DoorRunner::postAsSysop($this->db, $this->settings, $this->slug, $areaTag, $subject, $lines);
    }
}
