<?php
/**
 * VR票务插件 - 基础服务
 *
 * @package vr_ticket\service
 */

namespace app\plugins\vr_ticket\service;

class BaseService
{
    /**
     * 获取插件表前缀
     */
    public static function table($name)
    {
        return 'vr_' . $name;
    }

    /**
     * 获取当前时间戳
     */
    public static function now()
    {
        return time();
    }

    /**
     * 生成 UUID v4 票码
     */
    public static function generateUuid()
    {
        $data = random_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

    /**
     * AES-256-CBC 加密 QR 数据
     *
     * @param array $data 待加密数据
     * @param int|null $expire 过期时间戳（默认30天）
     * @return string base64 编码密文
     */
    public static function encryptQrData($data, $expire = null)
    {
        $secret = self::getQrSecret();
        $expire = $expire ?? (time() + 86400 * 30);

        $payload = json_encode(array_merge($data, [
            'exp' => $expire,
            'iat' => time(),
        ]), JSON_UNESCAPED_UNICODE);

        $iv = random_bytes(16);
        $encrypted = openssl_encrypt($payload, 'AES-256-CBC', $secret, OPENSSL_RAW_DATA, $iv);

        return base64_encode($iv . $encrypted);
    }

    /**
     * 解密 QR 数据
     *
     * @param string $encoded base64 编码密文
     * @return array|null
     */
    public static function decryptQrData($encoded)
    {
        $secret = self::getQrSecret();
        $combined = base64_decode($encoded);

        if (strlen($combined) < 16) {
            return null;
        }

        $iv = substr($combined, 0, 16);
        $encrypted = substr($combined, 16);

        $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $secret, OPENSSL_RAW_DATA, $iv);

        if ($decrypted === false) {
            return null;
        }

        $data = json_decode($decrypted, true);

        if (isset($data['exp']) && $data['exp'] < time()) {
            return null;
        }

        return $data;
    }

    /**
     * 获取 QR 加密密钥
     */
    private static function getQrSecret()
    {
        $secret = env('VR_TICKET_QR_SECRET', '');
        if (empty($secret)) {
            throw new \Exception('[vr_ticket] VR_TICKET_QR_SECRET 环境变量未配置，QR加密密钥不能为空。请在.env中设置VR_TICKET_QR_SECRET=<随机64字符字符串>');
        }
        return $secret;
    }

    /**
     * 判断商品是否为票务商品
     *
     * @param int $goods_id
     * @return bool
     */
    public static function isTicketGoods($goods_id)
    {
        $goods = \think\facade\Db::name('Goods')->find($goods_id);
        if (empty($goods)) {
            return false;
        }
        return !empty($goods['venue_data']) || ($goods['item_type'] ?? '') === 'ticket';
    }

    /**
     * 获取商品座位模板
     *
     * @param int $goods_id
     * @return array|null
     */
    public static function getSeatTemplateByGoods($goods_id)
    {
        $goods = \think\facade\Db::name('Goods')->find($goods_id);
        if (empty($goods) || empty($goods['category_id'])) {
            return null;
        }

        return \think\facade\Db::name(self::table('seat_templates'))
            ->where('category_id', $goods['category_id'])
            ->where('status', 1)
            ->find();
    }

    /**
     * 安全日志
     */
    public static function log($message, $context = [], $level = 'info')
    {
        $tag = '[vr_ticket]';
        $ctx = empty($context) ? '' : ' ' . json_encode($context, JSON_UNESCAPED_UNICODE);
        $log_func = "log_{$level}";
        if (function_exists($log_func)) {
            $log_func($tag . $message . $ctx);
        }
    }

    /**
     * 初始化票务商品规格
     *
     * 修复商品 112 的 broken 状态：
     * 1. 设置 is_exist_many_spec = 1（启用多规格模式）
     * 2. 插入 $vr- 规格类型（幂等，多次执行不重复）
     *
     * @param int $goodsId 商品ID
     * @return array ['code' => 0, 'msg' => '...', 'data' => [...]]
     */
    public static function initGoodsSpecs(int $goodsId): array
    {
        $goodsId = intval($goodsId);
        if ($goodsId <= 0) {
            return ['code' => -1, 'msg' => '商品ID无效'];
        }

        // 1. 检查商品是否存在
        $goods = \think\facade\Db::name('Goods')->where('id', $goodsId)->find();
        if (empty($goods)) {
            return ['code' => -2, 'msg' => "商品 {$goodsId} 不存在"];
        }

        $now = time();

        // 2. 启用多规格模式
        \think\facade\Db::name('Goods')->where('id', $goodsId)->update([
            'is_exist_many_spec' => 1,
            'upd_time' => $now,
        ]);

        // 3. 定义 $vr- 规格类型（5维：场次、场馆、演播室、分区、座位号）
        $specTypes = [
            '$vr-场次'   => '[{"name":"待选场次","images":""}]',
            '$vr-场馆'   => '[{"name":"' . ($goods['venue_data'] ?? '国家体育馆') . '","images":""}]',
            '$vr-演播室' => '[{"name":"主厅","images":""}]',
            '$vr-分区'   => '[{"name":"A区","images":""},{"name":"B区","images":""},{"name":"C区","images":""}]',
            '$vr-座位号' => '[{"name":"待选座位","images":""}]',
        ];

        $insertedCount = 0;
        foreach ($specTypes as $name => $value) {
            // 幂等：检查是否已存在
            $exists = \think\facade\Db::name('GoodsSpecType')
                ->where('goods_id', $goodsId)
                ->where('name', $name)
                ->find();

            if (empty($exists)) {
                \think\facade\Db::name('GoodsSpecType')->insert([
                    'goods_id' => $goodsId,
                    'name'     => $name,
                    'value'    => $value,
                    'add_time' => $now,
                ]);
                $insertedCount++;
                self::log('initGoodsSpecs: inserted spec_type', ['goods_id' => $goodsId, 'name' => $name]);
            }
        }

        self::log('initGoodsSpecs: done', ['goods_id' => $goodsId, 'inserted' => $insertedCount]);

        // 4. 返回当前所有 spec_type，便于验证
        $specTypes = \think\facade\Db::name('GoodsSpecType')
            ->where('goods_id', $goodsId)
            ->order('id', 'asc')
            ->select()
            ->toArray();

        return [
            'code' => 0,
            'msg'  => "初始化完成，插入 {$insertedCount} 条规格类型",
            'data' => [
                'goods_id'        => $goodsId,
                'is_exist_many_spec' => 1,
                'spec_types'      => $specTypes,
            ],
        ];
    }

    /**
     * 插件后台权限菜单
     *
     * ShopXO 通过 PluginsService::PluginsAdminPowerMenu() 调用此方法
     * 返回格式：二维数组，每项代表一个菜单分组
     *
     * @return array
     */
    public static function AdminPowerMenu()
    {
        return [
            // 座位模板
            [
                'name'    => '座位模板',
                'control' => 'seat_template',
                'action'  => 'list',
                'item'    => [
                    ['name' => '座位模板', 'action' => 'list'],
                    ['name' => '添加模板', 'action' => 'save'],
                ],
            ],
            // 电子票
            [
                'name'    => '电子票',
                'control' => 'ticket',
                'action'  => 'list',
                'item'    => [
                    ['name' => '电子票列表', 'action' => 'list'],
                    ['name' => '票详情',     'action' => 'detail'],
                    ['name' => '手动核销',   'action' => 'verify'],
                    ['name' => '导出票',     'action' => 'export'],
                ],
            ],
            // 核销员
            [
                'name'    => '核销员',
                'control' => 'verifier',
                'action'  => 'list',
                'item'    => [
                    ['name' => '核销员列表', 'action' => 'list'],
                    ['name' => '添加核销员', 'action' => 'save'],
                ],
            ],
            // 核销记录
            [
                'name'    => '核销记录',
                'control' => 'verification',
                'action'  => 'list',
                'item'    => [
                    ['name' => '核销记录', 'action' => 'list'],
                ],
            ],
        ];
    }

    /**
     * Phase 4: Feistel-8 混淆 + QR签名 + 短码编解码
     * ================================================================
     */

    /**
     * 获取 VR Ticket 主密钥
     * @throws \Exception 未配置密钥时抛出异常
     */
    private static function getVrSecret(): string
    {
        // $secret = env('VR_TICKET_SECRET', '');
        // 测试密钥
        $secret = '8935b3a3-a7b4-4e3d-8c1f-9b7e2a6f5d4c';
        if (empty($secret)) {
            throw new \Exception('[vr_ticket] VR_TICKET_SECRET 环境变量未配置！请在 .env 中设置 VR_TICKET_SECRET=<随机64字符字符串> 以确保票务安全');
        }
        return $secret;
    }

    /**
     * 获取 per-goods key
     * 由 master_secret 派生，保证不同商品的编码互相独立
     *
     * @param int $goods_id
     * @return string 16字节hex
     */
    public static function getGoodsKey(int $goods_id): string
    {
        static $cache = [];
        if (!isset($cache[$goods_id])) {
            $secret = self::getVrSecret();
            // HMAC-SHA256(master_secret, goods_id) 取前16字节
            $cache[$goods_id] = substr(hash_hmac('sha256', (string)$goods_id, $secret), 0, 16);
        }
        return $cache[$goods_id];
    }

    /**
     * Feistel Round 函数
     * F(R, i, key) = HMAC-SHA256(R . i, key) 的低19bit
     *
     * @param int $R 17bit 右半部分
     * @param int $round 轮次 [0-7]
     * @param string $key per-goods key
     * @return int 19bit 输出
     */
    private static function feistelRound(int $R, int $round, string $key): int
    {
        $hmac = hash_hmac('sha256', $R . '.' . $round, $key, true);
        // 取前3字节(24bit)，保留低19bit
        $val = (ord($hmac[0]) << 16) | (ord($hmac[1]) << 8) | ord($hmac[2]);
        return $val & 0x7FFFF; // 19bit mask
    }

    /**
     * 混淆编码（HMAC-XOR，保证可逆）
     *
     * @param int $packed 输入整数
     * @param string $key per-goods key
     * @return string base36编码
     */
    public static function feistelEncode(int $packed, string $key): string
    {
        // 对 36-bit 输入进行 8 轮 HMAC-XOR 混淆
        $L = ($packed >> 19) & 0x1FFFFF;
        $R = $packed & 0x7FFFF;

        for ($i = 0; $i < 8; $i++) {
            // 生成轮密钥
            $round_key = hash_hmac('sha256', pack('V', $i), $key, true);
            $F = (ord($round_key[0]) << 16) | (ord($round_key[1]) << 8) | ord($round_key[2]);

            // XOR 交换
            $L_new = $R;
            $R_new = ($L ^ $F) & 0x7FFFF;
            $L = $L_new;
            $R = $R_new;
        }

        // 合并
        $result = (($L & 0x1FFFFF) << 19) | ($R & 0x7FFFF);
        return base_convert($result, 10, 36);
    }

    /**
     * 混淆解码（与 encode 相同，XOR 本身可逆）
     *
     * @param string $code base36编码
     * @param string $key per-goods key
     * @return int 整数
     */
    public static function feistelDecode(string $code, string $key): int
    {
        $packed = intval(base_convert(strtolower($code), 36, 10));

        // 分离 L 和 R
        $L = ($packed >> 19) & 0x1FFFFF;
        $R = $packed & 0x7FFFF;

        // 8轮 XOR 混淆（与 encode 相同顺序，XOR 本身可逆）
        for ($i = 0; $i < 8; $i++) {
            $round_key = hash_hmac('sha256', pack('V', $i), $key, true);
            $F = (ord($round_key[0]) << 16) | (ord($round_key[1]) << 8) | ord($round_key[2]);

            $L_new = $R;
            $R_new = ($L ^ $F) & 0x7FFFF;
            $L = $L_new;
            $R = $R_new;
        }

        // 合并
        return (($L & 0x1FFFFF) << 19) | ($R & 0x7FFFF);
    }

    /**
     * 生成短码
     *
     * 编码结构：【明文4位 goods_id】【变长混淆 ticket_id】
     * - 前4位：goods_id 明文 base36，固定4位（范围 0-1,679,615）
     * - 后部：ticket_id 可变长度 base36，随 ticket_id 增长自动变长
     * - 解码 O(1)：前4位=goods_id，剩余全部=ticket_id（无需固定分隔）
     *
     * @param int $goods_id 0-1679615
     * @param int $ticket_id 任意正整数（可变长度）
     * @return string base36短码
     * @throws \Exception goods_id 超范围时抛出
     */
    public static function shortCodeEncode(int $goods_id, int $ticket_id): string
    {
        // 校验 goods_id 不超过 4位 base36 (0xFFFFFF = 1679615)
        if ($goods_id > 0xFFFFFF) {
            throw new \Exception("goods_id 超出范围 (max=1679615), given={$goods_id}");
        }
        if ($ticket_id <= 0) {
            throw new \Exception("ticket_id 必须为正整数, given={$ticket_id}");
        }

        // goods_id 固定4位 base36（明文）
        $goods_part = str_pad(base_convert($goods_id, 10, 36), 4, '0', STR_PAD_LEFT);


        // ticket_id 可变长度（不填充）
        $ticket_part = base_convert($ticket_id, 10, 36);

        // ticket_id 混淆
        $ticket_int = intval($ticket_part, 36);
        $key = self::getGoodsKey($goods_id);
        $obfuscated = self::feistelEncode($ticket_int, $key);

        // 拼接：前4位明文 goods_id + 变长混淆 ticket_id
        return strtolower($goods_part . $obfuscated);
    }

    /**
     * 解析短码（解码回 goods_id + ticket_id）
     *
     * 解码结构：【明文4位 goods_id】【变长混淆 ticket_id】
     * - 前4位：直接 base36_decode = goods_id
     * - 剩余全部：用 goods_id 派生 key → Feistel 解密 = ticket_id
     * - 解码 O(1)，无暴力搜索
     *
     * @param string $code 短码（小写或大写均可）
     * @param int|null $goods_id_hint 可选提示（已不需要，用于兼容）
     * @return array ['goods_id' => int, 'ticket_id' => int]
     * @throws \Exception 解码失败时抛出
     */
    public static function shortCodeDecode(string $code, ?int $goods_id_hint = null): array
    {
        $code = strtolower($code);
        // 前4位：明文 goods_id
        $goods_part = substr($code, 0, 4);
        $goods_id = intval($goods_part, 36);

        // 校验 hint（如果提供）
        if ($goods_id_hint !== null && $goods_id !== $goods_id_hint) {
            throw new \Exception("短码解码失败：hint 不匹配 (code={$code}, hint={$goods_id_hint}, decoded={$goods_id})");
        }

        // 用 goods_id 派生 key
        $key = self::getGoodsKey($goods_id);

        // 后部：变长混淆 ticket_id → Feistel 解密
        $ticket_part = substr($code, 4);
        $ticket_int = self::feistelDecode($ticket_part, $key);
        // 转回 base36 字符串（不填充）
        $ticket_id = intval(base_convert($ticket_int, 10, 36), 36);

        return [
            'goods_id' => $goods_id,
            'ticket_id' => $ticket_id,
        ];
    }

    /**
     * 签名 QR payload（HMAC-SHA256 防篡改）
     *
     * @param array $payload ['id'=>int, 'g'=>int(goods_id), 'iat'=>int, 'exp'=>int]
     * @return string base64编码的签名内容
     */
    public static function signQrPayload(array $payload): string
    {
        $secret = self::getVrSecret();
        // 签名内容：code.id.g.iat.exp（code = ticket_code）
        $sign_str = $payload['code'] . '.' . $payload['id'] . '.' . $payload['g'] . '.' . $payload['iat'] . '.' . $payload['exp'];
        $sig = substr(hash_hmac('sha256', $sign_str, $secret), 0, 8);
        $payload['sig'] = $sig;

        return base64_encode(json_encode($payload, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 验证 QR payload
     *
     * @param string $encoded base64编码
     * @return array|null 验证失败返回null，成功返回 payload（含id/g/exp）
     */
    public static function verifyQrPayload(string $encoded)
    {
        $json = base64_decode($encoded);
        if ($json === false) {
            return null;
        }

        $payload = json_decode($json, true);
        if (!is_array($payload)) {
            return null;
        }

        // 必填字段检查（包含 ticket_code）
        if (!isset($payload['id'], $payload['g'], $payload['code'], $payload['iat'], $payload['exp'], $payload['sig'])) {
            return null;
        }

        // 时间戳检查：是否过期
        if ($payload['exp'] < time()) {
            return null;
        }

        // 签名验证（包含 ticket_code）
        $secret = self::getVrSecret();
        $sign_str = $payload['code'] . '.' . $payload['id'] . '.' . $payload['g'] . '.' . $payload['iat'] . '.' . $payload['exp'];
        $expected_sig = substr(hash_hmac('sha256', $sign_str, $secret), 0, 8);

        if (!hash_equals($expected_sig, $payload['sig'])) {
            return null;
        }

        return [
            'id'   => intval($payload['id']),
            'g'    => intval($payload['g']),
            'code' => $payload['code'],
            'exp'  => intval($payload['exp']),
        ];
    }
}
