// Package gateway 提供流量卡相关的 7 个 API 方法封装
package gateway

import (
	"context"

	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/bytedance/sonic"
)

// QueryCardStatus 查询流量卡状态
func (c *Client) QueryCardStatus(ctx context.Context, req *CardStatusReq) (*CardStatusResp, error) {
	businessData := map[string]interface{}{
		"params": map[string]interface{}{
			"cardNo": req.CardNo,
		},
	}

	resp, err := c.doRequest(ctx, "/flow-card/status", businessData)
	if err != nil {
		return nil, err
	}

	var result CardStatusResp
	if err := sonic.Unmarshal(resp, &result); err != nil {
		return nil, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析卡状态响应失败")
	}

	return &result, nil
}

// QueryFlow 查询流量使用情况
func (c *Client) QueryFlow(ctx context.Context, req *FlowQueryReq) (*FlowUsageResp, error) {
	businessData := map[string]interface{}{
		"params": map[string]interface{}{
			"cardNo": req.CardNo,
		},
	}

	resp, err := c.doRequest(ctx, "/flow-card/flow", businessData)
	if err != nil {
		return nil, err
	}

	var result FlowUsageResp
	if err := sonic.Unmarshal(resp, &result); err != nil {
		return nil, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析流量使用响应失败")
	}

	return &result, nil
}

// QueryRealnameStatus 查询实名认证状态
func (c *Client) QueryRealnameStatus(ctx context.Context, req *CardStatusReq) (*RealnameStatusResp, error) {
	businessData := map[string]interface{}{
		"params": map[string]interface{}{
			"cardNo": req.CardNo,
		},
	}

	resp, err := c.doRequest(ctx, "/flow-card/realname-status", businessData)
	if err != nil {
		return nil, err
	}

	var result RealnameStatusResp
	if err := sonic.Unmarshal(resp, &result); err != nil {
		return nil, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析实名认证状态响应失败")
	}

	return &result, nil
}

// StopCard 流量卡停机
func (c *Client) StopCard(ctx context.Context, req *CardOperationReq) error {
	businessData := map[string]interface{}{
		"params": map[string]interface{}{
			"cardNo": req.CardNo,
		},
	}
	if req.Extend != "" {
		businessData["params"].(map[string]interface{})["extend"] = req.Extend
	}

	_, err := c.doRequest(ctx, "/flow-card/cardStop", businessData)
	return err
}

// StartCard 流量卡复机
func (c *Client) StartCard(ctx context.Context, req *CardOperationReq) error {
	businessData := map[string]interface{}{
		"params": map[string]interface{}{
			"cardNo": req.CardNo,
		},
	}
	if req.Extend != "" {
		businessData["params"].(map[string]interface{})["extend"] = req.Extend
	}

	_, err := c.doRequest(ctx, "/flow-card/cardStart", businessData)
	return err
}

// GetRealnameLink 获取实名认证跳转链接
func (c *Client) GetRealnameLink(ctx context.Context, req *CardStatusReq) (*RealnameLinkResp, error) {
	businessData := map[string]interface{}{
		"params": map[string]interface{}{
			"cardNo": req.CardNo,
		},
	}

	resp, err := c.doRequest(ctx, "/flow-card/realname-link", businessData)
	if err != nil {
		return nil, err
	}

	var result RealnameLinkResp
	if err := sonic.Unmarshal(resp, &result); err != nil {
		return nil, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析实名认证链接响应失败")
	}

	return &result, nil
}

// BatchQuery 批量查询（预留接口，暂未实现）
func (c *Client) BatchQuery(ctx context.Context, req *BatchQueryReq) (*BatchQueryResp, error) {
	return nil, errors.New(errors.CodeGatewayError, "批量查询接口暂未实现")
}
