package client

import "encoding/json"

// ListenPort is one TCP/UDP listener entry from the T1049 / T1046 port scan.
type ListenPort struct {
	Port    int    `json:"port"`
	Addr    string `json:"addr"`             // bind address: "0.0.0.0", "::", "127.0.0.1", etc.
	Proto   string `json:"proto"`            // "tcp" | "udp"
	Process string `json:"process,omitempty"` // process name if available
	PID     int    `json:"pid,omitempty"`
}

// ListenPortsReport is the structured payload returned by the listen_ports command.
type ListenPortsReport struct {
	Ports []ListenPort `json:"ports"`
	Count int          `json:"count"`
}

func (r *ListenPortsReport) JSON() string {
	if r == nil {
		return `{"ports":[],"count":0}`
	}
	r.Count = len(r.Ports)
	b, _ := json.Marshal(r)
	return string(b)
}

// PatchStatusReport is the simplified payload returned by the patch_status command.
type PatchStatusReport struct {
	PendingUpdates *int    `json:"pending_updates,omitempty"`
	LastPatch      *string `json:"last_patch,omitempty"`
	LastPatchDays  *int    `json:"last_patch_days,omitempty"`
	RebootPending  *bool   `json:"reboot_pending,omitempty"`
}

func collectPatchStatus() *PatchStatusReport {
	p := collectPosture()
	if p == nil {
		return &PatchStatusReport{}
	}
	return &PatchStatusReport{
		PendingUpdates: p.PendingUpdates,
		LastPatch:      p.LastPatch,
		LastPatchDays:  p.LastPatchDays,
		RebootPending:  p.RebootPending,
	}
}

func (r *PatchStatusReport) JSON() string {
	b, _ := json.Marshal(r)
	return string(b)
}
