<?php

/**
 * 文件功能：微信机器人接口调用服务
 *
 * 封装与微信机器人 HTTP API 的通信逻辑，用于向上游服务发送文本和富文本消息。
 *
 * @author   ChatRoom Laravel
 *
 * @version  1.0.0
 */

namespace App\Services\WechatBot;

use App\Models\SysParam;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class WechatBotApiService
{
    /**
     * @var string API 基础地址
     */
    protected string $baseUrl;

    /**
     * @var int 请求超时时间（秒）
     */
    protected int $timeout = 10;

    /**
     * @var string 机器人 Key
     */
    protected string $botKey;

    /**
     * 构造函数 — 从 SysParam 获取 api_base_url 配置
     */
    public function __construct()
    {
        $param = SysParam::where('alias', 'wechat_bot_config')->first();
        if ($param && ! empty($param->body)) {
            $config = json_decode($param->body, true);
            $this->baseUrl = rtrim($config['api']['base_url'] ?? '', '/');
            $this->botKey = $config['api']['bot_key'] ?? '';
        } else {
            $this->baseUrl = '';
            $this->botKey = '';
        }
    }

    /**
     * 发送文本消息
     *
     * @param  string  $toUser  目标用户 wxid 或群聊 ID
     * @param  string  $content  文本内容
     * @param  array  $atList  群聊中 @ 的用户 wxid 列表
     * @return array{success: bool, data: array|null, error: string|null}
     */
    public function sendTextMessage(string $toUser, string $content, array $atList = []): array
    {
        if (empty($this->baseUrl)) {
            return ['success' => false, 'data' => null, 'error' => 'API Base URL is not configured.'];
        }

        $url = "{$this->baseUrl}/message/SendTextMessage";

        $finalContent = "[和平聊吧]\n".$content;

        $payload = [
            'MsgItem' => [
                [
                    'AtWxIDList' => $atList,
                    'ImageContent' => '',
                    'MsgType' => 0,
                    'TextContent' => $finalContent,
                    'ToUserName' => $toUser,
                ],
            ],
        ];

        try {
            $response = Http::timeout($this->timeout)
                ->withHeaders([
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json',
                ])
                ->post("{$url}?key={$this->botKey}", $payload);

            if ($response->successful()) {
                $data = $response->json();

                if (($data['Code'] ?? 0) === 200) {
                    Log::info('微信机器人消息发送成功', [
                        'to_user' => $toUser,
                        'content' => mb_substr($content, 0, 50),
                    ]);

                    return ['success' => true, 'data' => $data['Data'] ?? [], 'error' => null];
                }

                $desc = $data['Text'] ?? 'Unknown';

                return [
                    'success' => false,
                    'data' => null,
                    'error' => "API 返回错误: Code={$data['Code']}, Text={$desc}",
                ];
            }

            return [
                'success' => false,
                'data' => null,
                'error' => "HTTP 请求失败: {$response->status()}",
            ];
        } catch (\Exception $e) {
            Log::error('微信机器人消息发送异常', [
                'to_user' => $toUser,
                'error' => $e->getMessage(),
            ]);

            return ['success' => false, 'data' => null, 'error' => '发送异常: '.$e->getMessage()];
        }
    }
}
