package handlers

import (
	"github.com/gofiber/fiber/v2"
	"proxy-server/gateway/app"
	"proxy-server/gateway/core"
)

type InfoResp struct {
	Id   int32  `json:"id"`
	Name string `json:"name"`

	FwdListeners    int `json:"fwd_listeners"`
	UserConnections int `json:"user_connections"`
	CtrlConnections int `json:"ctrl_connections"`
	DataConnections int `json:"data_connections"`

	// Edges []EdgeResp `json:"edges"`
	Edges   map[int32]*core.Edge   `json:"edges"`
	Permits map[int32]*core.Permit `json:"permits"`
}

type EdgeResp struct {
	Id     int32        `json:"id"`
	Port   uint16       `json:"port"`
	Permit *core.Permit `json:"permit"`
}

func Info(c *fiber.Ctx) error {

	var edges = make(map[int32]*core.Edge)
	app.Edges.Range(func(id int32, edge *core.Edge) bool {
		edges[id] = edge
		return true
	})

	var permits = make(map[int32]*core.Permit)
	app.Permits.Range(func(id int32, permit *core.Permit) bool {
		permits[id] = permit
		return true
	})

	return c.JSON(InfoResp{
		Id:              app.Id,
		Name:            app.Name,
		FwdListeners:    int(app.FwdLesWg.Count()),
		UserConnections: int(app.UserConnWg.Count()),
		CtrlConnections: int(app.CtrlConnWg.Count()),
		DataConnections: int(app.DataConnWg.Count()),
		Edges:           edges,
		Permits:         permits,
	})
}
