<?php
/**
 * 官方固定账号与官方通知。
 *
 * 固定账号用户名 101 只用于官方通知、弹窗公告与全站群发；
 * 不允许删除、代登录或修改用户名。
 */

/** 读取官方固定账号；不存在时返回 null */
function official_account()
{
    static $cached = false;
    if ($cached !== false) {
        return $cached;
    }
    try {
        $row = DB::one('SELECT * FROM ' . DB::table('users') . ' WHERE is_official = 1 AND status = 1 ORDER BY id ASC LIMIT 1');
        if (!$row) {
            $row = DB::one('SELECT * FROM ' . DB::table('users') . " WHERE username IN ('101','01') ORDER BY id ASC LIMIT 1");
        }
        $cached = $row ?: null;
    } catch (Exception $e) {
        $cached = null;
    }
    return $cached;
}

function official_account_id()
{
    $user = official_account();
    return $user ? (int) $user['id'] : 0;
}

function is_official_account($user)
{
    if (!$user || !is_array($user)) {
        return false;
    }
    if ((int) ($user['is_official'] ?? 0) === 1) {
        return true;
    }
    // 兼容旧库：固定登录名 101/01 即官方账号
    return in_array((string) ($user['username'] ?? ''), ['101', '01'], true) && official_account_id() === (int) ($user['id'] ?? 0);
}

/** 官方账号的展示认证信息（用于弹窗/公告） */
function official_verify_meta()
{
    return [
        'verified' => 1,
        'verify_status' => 2,
        'verify_type' => 'official',
        'verify_style' => 'gold',
        'verify_title' => '官方账号',
        'verify_info' => '木漏茶室官方通知账号',
    ];
}
