<?php

namespace App\Services;

use App\Constants\HttpStatus;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\App;

class PublicService extends BaseService
{
    public static function init($telegram, $data, $chatId, $firstName, $messageId): void
    {
        switch ($data) {
            case "message@@close"://关闭本条消息
                $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
                break;
        }
    }

    /**
     * 获取Tg用户的 id 昵称 用户名
     * @param $message
     * @param $from
     * @return array
     */
    public static function getChatInfo($message, $from): array
    {
        $chatId = $message->chat->id;
        $firstName = $message->chat->firstName;
        $username = '';
        if (!$from->isBot) {
            $chatId = $from->id;
            $firstName = $from->firstName;
            $username = $from->username;
        }
        return [$chatId, $firstName, $username];
    }

    /**
     * 用户初始化 （用户注册）/ 更新用户昵称
     * @param $chatId
     * @param $username
     * @param $firstName
     * @return User
     */
    public static function index($chatId, $username, $firstName): User
    {

        $user = User::where('member_id', $chatId)->first();
        if (!$user) {
            $user = new User();
            $user->from = -1;
            $user->user_code = User::generateUserCode();
        }
        $user->member_id = $chatId;
        $user->first_name = $firstName;
        if ($username) $user->username = $username;
        if (empty($user->user_id)) {
            $user->user_id = $chatId;
        }
        $user->save();

        //给每个用户生成一个专属的钱包
        WalletService::getUserWallet($chatId);

        return $user;
    }

    public static function getWhere(array $search = []): array
    {
        // TODO: Implement getWhere() method.
        return [];
    }
}