package model

import "github.com/uptrace/bun"

const Version int = 3

type Vulnerability struct {
	bun.BaseModel `bun:"table:vulnerability,alias:v"`
	ID            int64       `json:"id" bun:"id,pk,autoincrement"`
	Package       string      `json:"package,omitempty"`
	CVE           string      `json:"cve,omitempty"`
	Source        string      `json:"source,omitempty"`
	Constraints   string      `json:"constraints,omitempty"`
	CPE           []string    `json:"cpes,omitempty"` // Common Platform Enumeration
	Distro        string      `json:"distro,omitempty"`
	Ecosystem     string      `json:"ecosystem,omitempty"` // Ecosystem is the package manager or the source of the package
	DistroVersion string      `json:"distro_version,omitempty"`
	Vendor        string      `json:"vendor,omitempty"`
	Qualifiers    string      `json:"qualifiers,omitempty"` // Purl Qualifiers determines the package is qualified for the match analysis based on the source
	Severity      string      `json:"severity,omitempty"`
	CVSS          []CVSS      `json:"cvss,omitempty"`
	EPSS          EPSS        `json:"epss,omitempty"`
	Description   string      `json:"description,omitempty"`
	References    []Reference `json:"references,omitempty"`
	Fixes         []string    `json:"fix,omitempty"` // Fix is the version that the package should be updated to
	Advisories    []string    `json:"advisories,omitempty"`
	Properties    []Property  `json:"properties,omitempty"`
}

// Catalog : Metadata for the vulnerability database
type Catalog struct {
	bun.BaseModel   `bun:"table:catalog,alias:c"`
	ID              int64  `json:"id" bun:"id,pk,autoincrement"`
	Source          string `json:"source,omitempty"`
	SchemaVersion   int    `json:"schema,omitempty"`
	Vulnerabilities int    `json:"vulnerabilities,omitempty"`
	UniqueCVECount  int    `json:"unique_cve_count,omitempty"`
	GeneratedAt     string `json:"generated_at,omitempty"`
}

type Reference struct {
	Source string `json:"source"`
	URL    string `json:"url"`
}

// CVSS : Common Vulnerability Scoring System
type CVSS struct {
	Source              string  `json:"source"`
	Method              string  `json:"method"`
	Severity            string  `json:"severity"`
	Score               float64 `json:"score"`
	Vector              string  `json:"vector"`
	ExploitabilityScore float64 `json:"exploitability_score"`
	ImpactScore         float64 `json:"impact_score"`
}

// Exploit : Proof of Concept (PoC) data or Exploit data
type Exploit struct {
	Source      string `json:"source"`
	URL         string `json:"url"`
	Description string `json:"description"`
}

// EPSS : Exploitability Prediction Scoring System
type EPSS struct {
	URL        string `json:"url,omitempty"`
	Score      string `json:"score,omitempty"`
	Percentile string `json:"percentile,omitempty"`
	Date       string `json:"date,omitempty"`
}

// Fix : Available Remediation Data
type Fix struct {
	Source string `json:"source"`
	Value  string `json:"value"`
}

// Property : Additional metadata
type Property struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}
