<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\src\Mcp\Log;

/**
 * The four log sources `read_logs` can answer from, and the exact German column names each one
 * lives behind.
 *
 * Every column name in {@see columns()} was checked against the live dev schema, not against the
 * design doc — `read_logs` interpolates these names straight into SQL (they are the only strings
 * in the whole layer that ever are), so a typo here is a fatal query, and a name taken from a
 * table that was migrated since would be worse: a silent empty result.
 */
enum LogSource: string
{
    case System   = 'system';
    case Settings = 'settings';
    case Payment  = 'payment';
    case AuthFile = 'auth_file';

    /**
     * Table, keyset columns and the message/channel columns of this source.
     *
     * `level` is null where the source has no severity at all — `teinstellungenlog` is an audit
     * trail, and inventing a level for it would let an LLM report "no errors in settings" as if
     * that meant something.
     *
     * @return array{table:string,id:string,time:string,message:string,channel:string,level:?string}
     */
    public function columns(): array
    {
        return match ($this) {
            self::System   => [
                'table'   => 'tjtllog',
                'id'      => 'kLog',
                'time'    => 'dErstellt',
                'message' => 'cLog',
                'channel' => 'cKey',
                'level'   => 'nLevel',
            ],
            self::Settings => [
                'table'   => 'teinstellungenlog',
                'id'      => 'kEinstellungenLog',
                'time'    => 'dDatum',
                // The setting name is both the "message" and the "channel" here: it is the only
                // free-text-ish column, and it is what the contract's `channel` filter targets.
                'message' => 'cEinstellungenName',
                'channel' => 'cEinstellungenName',
                'level'   => null,
            ],
            self::Payment  => [
                'table'   => 'tzahlungslog',
                'id'      => 'kZahlunglog',
                'time'    => 'dDatum',
                'message' => 'cLog',
                'channel' => 'cModulId',
                'level'   => 'nLevel',
            ],
            // Not a table at all; the file reader owns it. Kept in the enum so the tool has one
            // source list and callers learn one schema.
            self::AuthFile => [
                'table'   => '',
                'id'      => '',
                'time'    => '',
                'message' => '',
                'channel' => '',
                'level'   => null,
            ],
        };
    }

    public function isDatabaseSource(): bool
    {
        return $this !== self::AuthFile;
    }

    /** @return list<string> */
    public static function names(): array
    {
        return \array_map(static fn (self $source): string => $source->value, self::cases());
    }
}
