syntax = "v1"

info (
	title:   "Node API"
	desc:    "API for ppanel"
	author:  "Tension"
	email:   "tension@ppanel.com"
	version: "0.0.1"
)

import "../types.api"

type (
	ServerOnlineIP {
		IP       string `json:"ip"`
		Protocol string `json:"protocol"`
	}
	ServerOnlineUser {
		IP          []ServerOnlineIP `json:"ip"`
		UserId      int64            `json:"user_id"`
		Subscribe   string           `json:"subscribe"`
		SubscribeId int64            `json:"subscribe_id"`
		Traffic     int64            `json:"traffic"`
		ExpiredAt   int64            `json:"expired_at"`
	}
	ServerStatus {
		Cpu      float64            `json:"cpu"`
		Mem      float64            `json:"mem"`
		Disk     float64            `json:"disk"`
		Protocol string             `json:"protocol"`
		Online   []ServerOnlineUser `json:"online"`
		Status   string             `json:"status"`
	}
	Server {
		Id             int64        `json:"id"`
		Name           string       `json:"name"`
		Country        string       `json:"country"`
		City           string       `json:"city"`
		Address        string       `json:"address"`
		Sort           int          `json:"sort"`
		Protocols      []Protocol   `json:"protocols"`
		LastReportedAt int64        `json:"last_reported_at"`
		Status         ServerStatus `json:"status"`
		CreatedAt      int64        `json:"created_at"`
		UpdatedAt      int64        `json:"updated_at"`
	}
	CreateServerRequest {
		Name      string     `json:"name"`
		Country   string     `json:"country,omitempty"`
		City      string     `json:"city,omitempty"`
		Address   string     `json:"address"`
		Sort      int        `json:"sort,omitempty"`
		Protocols []Protocol `json:"protocols"`
	}
	UpdateServerRequest {
		Id        int64      `json:"id"`
		Name      string     `json:"name"`
		Country   string     `json:"country,omitempty"`
		City      string     `json:"city,omitempty"`
		Address   string     `json:"address"`
		Sort      int        `json:"sort,omitempty"`
		Protocols []Protocol `json:"protocols"`
	}
	DeleteServerRequest {
		Id int64 `json:"id"`
	}
	FilterServerListRequest {
		Page   int    `form:"page"`
		Size   int    `form:"size"`
		Search string `form:"search,omitempty"`
	}
	FilterServerListResponse {
		Total int64    `json:"total"`
		List  []Server `json:"list"`
	}
	GetServerProtocolsRequest {
		Id int64 `form:"id"`
	}
	GetServerProtocolsResponse {
		Protocols []Protocol `json:"protocols"`
	}
	Node {
		Id           int64    `json:"id"`
		Name         string   `json:"name"`
		Tags         []string `json:"tags"`
		Port         uint16   `json:"port"`
		Address      string   `json:"address"`
		ServerId     int64    `json:"server_id"`
		Protocol     string   `json:"protocol"`
		Enabled      *bool    `json:"enabled"`
		Sort         int      `json:"sort,omitempty"`
		NodeGroupId  int64    `json:"node_group_id,omitempty"`
		NodeGroupIds []int64  `json:"node_group_ids,omitempty"`
		CreatedAt    int64    `json:"created_at"`
		UpdatedAt    int64    `json:"updated_at"`
	}
	CreateNodeRequest {
		Name         string   `json:"name"`
		Tags         []string `json:"tags,omitempty"`
		Port         uint16   `json:"port"`
		Address      string   `json:"address"`
		ServerId     int64    `json:"server_id"`
		Protocol     string   `json:"protocol"`
		Enabled      *bool    `json:"enabled"`
		NodeGroupIds []int64  `json:"node_group_ids,omitempty"`
	}
	UpdateNodeRequest {
		Id           int64    `json:"id"`
		Name         string   `json:"name"`
		Tags         []string `json:"tags,omitempty"`
		Port         uint16   `json:"port"`
		Address      string   `json:"address"`
		ServerId     int64    `json:"server_id"`
		Protocol     string   `json:"protocol"`
		Enabled      *bool    `json:"enabled"`
		NodeGroupIds []int64  `json:"node_group_ids,omitempty"`
	}
	ToggleNodeStatusRequest {
		Id     int64 `json:"id"`
		Enable *bool `json:"enable"`
	}
	DeleteNodeRequest {
		Id int64 `json:"id"`
	}
	FilterNodeListRequest {
		Page        int    `form:"page"`
		Size        int    `form:"size"`
		Search      string `form:"search,omitempty"`
		NodeGroupId *int64 `form:"node_group_id,omitempty"`
	}
	FilterNodeListResponse {
		Total int64  `json:"total"`
		List  []Node `json:"list"`
	}
	HasMigrateSeverNodeResponse {
		HasMigrate bool `json:"has_migrate"`
	}
	MigrateServerNodeResponse {
		Succee  uint64 `json:"succee"`
		Fail    uint64 `json:"fail"`
		Message string `json:"message,omitempty"`
	}
	ResetSortRequest {
		Sort []SortItem `json:"sort"`
	}
	QueryNodeTagResponse {
		Tags []string `json:"tags"`
	}
)

@server (
	prefix:     v1/admin/server
	group:      admin/server
	middleware: AuthMiddleware
)
service ppanel {
	@doc "Create Server"
	@handler CreateServer
	post /create (CreateServerRequest)

	@doc "Update Server"
	@handler UpdateServer
	post /update (UpdateServerRequest)

	@doc "Delete Server"
	@handler DeleteServer
	post /delete (DeleteServerRequest)

	@doc "Filter Server List"
	@handler FilterServerList
	get /list (FilterServerListRequest) returns (FilterServerListResponse)

	@doc "Get Server Protocols"
	@handler GetServerProtocols
	get /protocols (GetServerProtocolsRequest) returns (GetServerProtocolsResponse)

	@doc "Create Node"
	@handler CreateNode
	post /node/create (CreateNodeRequest)

	@doc "Update Node"
	@handler UpdateNode
	post /node/update (UpdateNodeRequest)

	@doc "Delete Node"
	@handler DeleteNode
	post /node/delete (DeleteNodeRequest)

	@doc "Filter Node List"
	@handler FilterNodeList
	get /node/list (FilterNodeListRequest) returns (FilterNodeListResponse)

	@doc "Toggle Node Status"
	@handler ToggleNodeStatus
	post /node/status/toggle (ToggleNodeStatusRequest)

	@doc "Reset server sort"
	@handler ResetSortWithServer
	post /server/sort (ResetSortRequest)

	@doc "Reset node sort"
	@handler ResetSortWithNode
	post /node/sort (ResetSortRequest)

	@doc "Query all node tags"
	@handler QueryNodeTag
	get /node/tags returns (QueryNodeTagResponse)
}

