package server

import "time"

const (
	CollectStatusQueued   = "queued"
	CollectStatusRunning  = "running"
	CollectStatusSuccess  = "success"
	CollectStatusFailed   = "failed"
	CollectStatusCanceled = "canceled"
)

type CollectRequest struct {
	Host             string `json:"host"`
	Protocol         string `json:"protocol"`
	Port             int    `json:"port"`
	Username         string `json:"username"`
	AuthType         string `json:"auth_type"`
	Password         string `json:"password,omitempty"`
	Token            string `json:"token,omitempty"`
	TLSMode          string `json:"tls_mode"`
	PowerOnIfHostOff bool   `json:"power_on_if_host_off,omitempty"`
}

type CollectProbeResponse struct {
	Reachable             bool   `json:"reachable"`
	Protocol              string `json:"protocol,omitempty"`
	HostPowerState        string `json:"host_power_state,omitempty"`
	HostPoweredOn         bool   `json:"host_powered_on"`
	PowerControlAvailable bool   `json:"power_control_available"`
	Message               string `json:"message,omitempty"`
}

type CollectJobResponse struct {
	JobID     string    `json:"job_id"`
	Status    string    `json:"status"`
	Message   string    `json:"message,omitempty"`
	CreatedAt time.Time `json:"created_at"`
}

type CollectJobStatusResponse struct {
	JobID     string    `json:"job_id"`
	Status    string    `json:"status"`
	Progress  *int      `json:"progress,omitempty"`
	Logs      []string  `json:"logs,omitempty"`
	Error     string    `json:"error,omitempty"`
	CreatedAt time.Time `json:"created_at,omitempty"`
	UpdatedAt time.Time `json:"updated_at"`
}

type CollectRequestMeta struct {
	Host     string `json:"host"`
	Protocol string `json:"protocol"`
	Port     int    `json:"port"`
	Username string `json:"username"`
	AuthType string `json:"auth_type"`
	TLSMode  string `json:"tls_mode"`
}

type Job struct {
	ID          string
	Status      string
	Progress    int
	Logs        []string
	Error       string
	CreatedAt   time.Time
	UpdatedAt   time.Time
	RequestMeta CollectRequestMeta
	cancel      func()
}

func (j *Job) toStatusResponse() CollectJobStatusResponse {
	progress := j.Progress
	resp := CollectJobStatusResponse{
		JobID:     j.ID,
		Status:    j.Status,
		Progress:  &progress,
		Logs:      append([]string(nil), j.Logs...),
		Error:     j.Error,
		CreatedAt: j.CreatedAt,
		UpdatedAt: j.UpdatedAt,
	}
	return resp
}

func (j *Job) toJobResponse(message string) CollectJobResponse {
	return CollectJobResponse{
		JobID:     j.ID,
		Status:    j.Status,
		Message:   message,
		CreatedAt: j.CreatedAt,
	}
}
