package jd

import (
	"encoding/json"
	"fmt"
)

func EdgeDevice(req EdgeDeviceReq) (*EdgeDeviceData, error) {
	path := "/edge/device"
	resp, err := Post(path, req)
	if err != nil {
		return nil, err
	}

	var respData EdgeDeviceResp
	if err := json.NewDecoder(resp).Decode(&respData); err != nil {
		return nil, fmt.Errorf("%s 响应解析失败: %v", path, err)
	}

	if respData.Code != 0 {
		return nil, fmt.Errorf("%s 响应失败: %s", path, respData.Meta)
	}

	return &respData.Data, nil
}

type EdgeDeviceReq struct {
	Geo    string `json:"geo"`
	Offset int    `json:"offset"`
	Num    int    `json:"num"`
}

type EdgeDeviceResp struct {
	Code int            `json:"code"`
	Meta string         `json:"meta"`
	Data EdgeDeviceData `json:"data"`
}

type EdgeDeviceData struct {
	Count int              `json:"count"`
	Edges []EdgeDeviceEdge `json:"edges"`
}

type EdgeDeviceEdge struct {
	Macaddr string `json:"macaddr"`
	Public  string `json:"public"`
	Isp     string `json:"isp"`
	Single  int    `json:"single"`
	Sole    bool   `json:"sole"`
	Arch    int    `json:"arch"`
	Online  int    `json:"online"`
}
