package service

import (
	"github.com/fleetdm/fleet/v4/server/fleet"
	"github.com/fleetdm/fleet/v4/server/ptr"
)

// GetHosts retrieves the list of all Hosts
func (c *Client) GetHosts(query string) ([]HostResponse, error) {
	verb, path := "GET", "/api/v1/fleet/hosts"
	var responseBody listHostsResponse
	err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query)
	return responseBody.Hosts, err
}

// HostByIdentifier retrieves a host by the uuid, osquery_host_id, hostname, or
// node_key.
func (c *Client) HostByIdentifier(identifier string) (*HostDetailResponse, error) {
	verb, path := "GET", "/api/v1/fleet/hosts/identifier/"+identifier
	var responseBody getHostResponse
	err := c.authenticatedRequest(nil, verb, path, &responseBody)
	return responseBody.Host, err
}

func (c *Client) translateTransferHostsToIDs(hosts []string, label string, team string) ([]uint, uint, uint, error) {
	verb, path := "POST", "/api/v1/fleet/translate"
	var responseBody translatorResponse

	var translatePayloads []fleet.TranslatePayload
	for _, host := range hosts {
		translatedPayload, err := encodeTranslatedPayload(fleet.TranslatorTypeHost, host)
		if err != nil {
			return nil, 0, 0, err
		}
		translatePayloads = append(translatePayloads, translatedPayload)
	}

	if label != "" {
		translatedPayload, err := encodeTranslatedPayload(fleet.TranslatorTypeLabel, label)
		if err != nil {
			return nil, 0, 0, err
		}
		translatePayloads = append(translatePayloads, translatedPayload)
	}

	translatedPayload, err := encodeTranslatedPayload(fleet.TranslatorTypeTeam, team)
	if err != nil {
		return nil, 0, 0, err
	}
	translatePayloads = append(translatePayloads, translatedPayload)

	params := translatorRequest{List: translatePayloads}

	err = c.authenticatedRequest(&params, verb, path, &responseBody)
	if err != nil {
		return nil, 0, 0, err
	}

	var hostIDs []uint
	var labelID uint
	var teamID uint

	for _, payload := range responseBody.List {
		switch payload.Type {
		case fleet.TranslatorTypeLabel:
			labelID = payload.Payload.ID
		case fleet.TranslatorTypeTeam:
			teamID = payload.Payload.ID
		case fleet.TranslatorTypeHost:
			hostIDs = append(hostIDs, payload.Payload.ID)
		}
	}
	return hostIDs, labelID, teamID, nil
}

func encodeTranslatedPayload(translatorType string, identifier string) (fleet.TranslatePayload, error) {
	translatedPayload := fleet.TranslatePayload{
		Type:    translatorType,
		Payload: fleet.StringIdentifierToIDPayload{Identifier: identifier},
	}
	return translatedPayload, nil
}

func (c *Client) TransferHosts(hosts []string, label string, status, searchQuery string, team string) error {
	hostIDs, labelID, teamID, err := c.translateTransferHostsToIDs(hosts, label, team)
	if err != nil {
		return err
	}

	if len(hosts) != 0 {
		verb, path := "POST", "/api/v1/fleet/hosts/transfer"
		var responseBody addHostsToTeamResponse
		params := addHostsToTeamRequest{TeamID: ptr.Uint(teamID), HostIDs: hostIDs}
		return c.authenticatedRequest(params, verb, path, &responseBody)
	}

	var labelIDPtr *uint
	if label != "" {
		labelIDPtr = &labelID
	}

	verb, path := "POST", "/api/v1/fleet/hosts/transfer/filter"
	var responseBody addHostsToTeamByFilterResponse
	params := addHostsToTeamByFilterRequest{TeamID: ptr.Uint(teamID), Filters: struct {
		MatchQuery string           `json:"query"`
		Status     fleet.HostStatus `json:"status"`
		LabelID    *uint            `json:"label_id"`
	}{MatchQuery: searchQuery, Status: fleet.HostStatus(status), LabelID: labelIDPtr}}
	return c.authenticatedRequest(params, verb, path, &responseBody)
}
