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

import (
	"context"

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

// QueryCardStatus 查询流量卡状态
// POST /flow-card/status
func (c *Client) QueryCardStatus(ctx context.Context, req *CardStatusReq) (*CardStatusResp, error) {
	return doRequestWithResponse[CardStatusResp](c, ctx, "/flow-card/status", req)
}

// QueryFlow 查询流量使用情况
// POST /flow-card/flow
func (c *Client) QueryFlow(ctx context.Context, req *FlowQueryReq) (*FlowUsageResp, error) {
	return doRequestWithResponse[FlowUsageResp](c, ctx, "/flow-card/flow", req)
}

// QueryRealnameStatus 查询实名认证状态
// POST /flow-card/realName
func (c *Client) QueryRealnameStatus(ctx context.Context, req *CardStatusReq) (*RealnameStatusResp, error) {
	return doRequestWithResponse[RealnameStatusResp](c, ctx, "/flow-card/realName", req)
}

// StopCard 流量卡停机
// POST /flow-card/cardStop
func (c *Client) StopCard(ctx context.Context, req *CardOperationReq) error {
	_, err := c.doRequest(ctx, "/flow-card/cardStop", req)
	return err
}

// StartCard 流量卡复机
// POST /flow-card/cardStart
func (c *Client) StartCard(ctx context.Context, req *CardOperationReq) error {
	_, err := c.doRequest(ctx, "/flow-card/cardStart", req)
	return err
}

// GetRealnameLink 获取实名认证跳转链接
// POST /flow-card/RealNameVerification
func (c *Client) GetRealnameLink(ctx context.Context, req *CardStatusReq) (*RealnameLinkResp, error) {
	return doRequestWithResponse[RealnameLinkResp](c, ctx, "/flow-card/RealNameVerification", req)
}

// SetCardSpeedTier 设置或恢复流量卡固定限速档位。
// POST /flow-card/speedLimit
func (c *Client) SetCardSpeedTier(ctx context.Context, req *CardSpeedTierReq) error {
	if req == nil || req.CardNo == "" || req.Code == "" {
		return errors.New(errors.CodeInvalidParam, "流量卡号和限速档位不能为空")
	}
	// 限速是外部状态写入，网络错误或超时后实际结果不可确定，禁止沿用查询接口的自动重试。
	requestClient := *c
	requestClient.maxRetries = 0
	_, err := requestClient.doRequest(ctx, "/flow-card/speedLimit", req)
	return err
}

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