package models

import "sync"

type Device struct {
    DeviceID            string      `json:"device_id"`
    Hostname            string      `json:"hostname,omitempty"`
    IP                  string      `json:"ip"`
    AgentPort           int         `json:"agent_port"`
    MediaPort           int         `json:"media_port"`
    DeviceAlias         string      `json:"device_alias,omitempty"`
    DeviceName          string      `json:"device_name"`
    InstanceName        string      `json:"instance_name,omitempty"`
    InstanceDisplayName string      `json:"instance_display_name,omitempty"`
    Version             string      `json:"version"`
    BuildID             string      `json:"build_id,omitempty"`
    GitSha              string      `json:"git_sha"`
    UptimeSec           int64       `json:"uptime_sec,omitempty"`
    LastSeenMs          int64       `json:"last_seen_ms"`
    Online              bool        `json:"online"`
    Graphs              interface{} `json:"graphs,omitempty"` // 摘要或详情
}

type DeviceRegistry struct {
    Mu      sync.RWMutex
    Devices map[string]*Device
}

func NewDeviceRegistry() *DeviceRegistry {
    return &DeviceRegistry{
        Devices: make(map[string]*Device),
    }
}

func (d *Device) DisplayName() string {
    if d == nil {
        return ""
    }
    if d.DeviceAlias != "" {
        return d.DeviceAlias
    }
    if d.DeviceName != "" {
        return d.DeviceName
    }
    if d.Hostname != "" {
        return d.Hostname
    }
    if d.DeviceID != "" {
        return d.DeviceID
    }
    return "-"
}

func (d *Device) TechnicalName() string {
    if d == nil {
        return ""
    }
    if d.Hostname != "" && d.Hostname != d.DeviceAlias {
        return d.Hostname
    }
    if d.DeviceName != "" && d.DeviceName != d.DeviceAlias {
        return d.DeviceName
    }
    return ""
}
