package models

import (
	"time"

	"go.mongodb.org/mongo-driver/v2/bson"
)

// Scan status values for ServerPackages.
const (
	ScanStatusOK          = "ok"
	ScanStatusUnsupported = "unsupported"
)

type OSRelease struct {
	Family    string `bson:"family" json:"family"`
	VersionID string `bson:"version_id" json:"version_id"`
	Arch      string `bson:"arch" json:"arch"`
}

type InstalledPackage struct {
	Name    string `bson:"name" json:"name"`
	Version string `bson:"version" json:"version"`
	Epoch   int    `bson:"epoch,omitempty" json:"epoch,omitempty"`
	Arch    string `bson:"arch" json:"arch"`
	// SourceName is what the Debian and Ubuntu feeds are keyed on. One advisory
	// against "openssl" covers the binaries libssl3, openssl and libssl-dev;
	// matching on binary name alone finds one of the three.
	SourceName string `bson:"source_name,omitempty" json:"source_name,omitempty"`
}

// ServerPackages holds one server's whole package set in ONE document rather
// than one document per package. The hash has already established that
// something changed, so a report is a single atomic upsert with no delta logic
// to get wrong. A typical Linux host is ~2000 packages and ~150KB, comfortably
// inside the 16MB document limit.
type ServerPackages struct {
	ID          bson.ObjectID      `bson:"_id,omitempty" json:"-"`
	InstanceID  string             `bson:"instance_id" json:"-"`
	ServerID    string             `bson:"server_id" json:"server_id"`
	OS          OSRelease          `bson:"os" json:"os"`
	Hash        string             `bson:"hash" json:"hash"`
	Packages    []InstalledPackage `bson:"packages" json:"packages"`
	CollectedAt time.Time          `bson:"collected_at" json:"collected_at"`
	ScanPending bool               `bson:"scan_pending" json:"scan_pending"`
	ScannedAt   time.Time          `bson:"scanned_at,omitempty" json:"scanned_at,omitempty"`
	// Status distinguishes a scanned host from one whose distribution we hold
	// no feed for. Reporting zero findings for an unsupported distribution is
	// indistinguishable from reporting a clean host, and one of those is a lie.
	Status    string `bson:"status" json:"status"`
	DBVersion int    `bson:"db_version" json:"db_version"`
}
