package com.red.circle.order.adapter.app;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.utility.BotUtils;
import com.red.circle.order.app.command.pay.web.strategy.TelegramPurchaseService;
import com.red.circle.order.app.dto.clientobject.pay.TelegramRechargeCO;
import com.red.circle.order.app.dto.cmd.TelegramRechargeCmd;
import com.red.circle.order.app.service.TelegramService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Description:
 *
 * @author 金泽强
 * @date 2024/12/10 22:53
 */
@Slf4j
//@RestController
@RequestMapping(value = "/telegram", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class TelegramController {

    private final TelegramPurchaseService telegramPurchaseService;

    private final TelegramService telegramService;
    private final Gson gson = BotUtils.GSON;

    /**
     * telegram hook
     */
    @PostMapping("/webhook")
    public void webhook(@RequestBody JSONObject jsonObject) {
        log.info("webhook：{}", jsonObject);
        Update update = gson.fromJson(jsonObject.toString(), Update.class);
        telegramService.dealWithUpdate(update);
    }

    /**
     * Telegram内购下单
     * @param cmd
     * @return
     */
    @PostMapping("/recharge")
    public TelegramRechargeCO telegram(@RequestBody @Validated TelegramRechargeCmd cmd) {
        return telegramPurchaseService.recharge(cmd);
    }

}
