from pip_audit_extra.vulnerability.filter.filter_option import FilterOption
from pip_audit_extra.vulnerability.filter.checker import Checker
from pip_audit_extra.vulnerability.dataclass import Vulnerability
from pip_audit_extra.severity import Severity, SEVERITY_PRIORITY


class SeverityFilterOption(FilterOption):
	__slots__ = "exac", "value"

	value: Severity

	def __init__(self, exac: bool, value: Severity) -> None:
		super().__init__(exac, value)


class SeverityChecker(Checker):
	"""
	Checks vulnerabilities for compliance with a certain severity priority.

	Notes:
		* A vulnerability is considered suitable if its severity corresponds to or exceeds that specified.
	"""
	__slots__ = "value"

	value: int

	def __init__(self, severity: Severity) -> None:
		if SEVERITY_PRIORITY:
			self.value = SEVERITY_PRIORITY.get(severity, max(SEVERITY_PRIORITY.values()) + 1)
		else:
			self.value = 0

	def check(self, vuln: Vulnerability) -> bool:
		if severity := vuln.severity:
			priority = SEVERITY_PRIORITY.get(severity)

			if priority is not None:
				return priority <= self.value

		return False


class SeverityExacChecker(Checker):
	"""
	Checks vulnerabilities for compliance with a certain severity.

	Notes:
		* A vulnerability is considered suitable if its severity corresponds to its specified.
	"""
	__slots__ = "value"

	value: Severity

	def __init__(self, value: Severity) -> None:
		self.value = value

	def check(self, vuln: Vulnerability) -> bool:
		return vuln.severity is self.value
