package models.odc

import com.ysoft.odc.{CvssRating, CWE}
import models.odc.profile.api._
import slick.lifted.Tag

case class Vulnerability (cve: String, description: String, cweOption: Option[CWE], cvss: CvssRating)
class Vulnerabilities(tag: Tag) extends Table[(Int, Vulnerability)](tag, "vulnerability") {
  def id = column[Int]("id")
  def cve = column[String]("cve")
  def description = column[String]("description")
  def cweOption = column[String]("cwe").?
  def cvssScore = column[Double]("cvssScore").?
  def authentication = column[String]("cvssAuthentication").?
  def availabilityImpact = column[String]("cvssAvailabilityImpact").?
  def accessVector = column[String]("cvssAccessVector").?
  def integrityImpact = column[String]("cvssIntegrityImpact").?
  def cvssAccessComplexity = column[String]("cvssAccessComplexity").?
  def cvssConfidentialityImpact = column[String]("cvssConfidentialityImpact").?

  def cvssRating = (cvssScore, authentication, availabilityImpact, accessVector, integrityImpact, cvssAccessComplexity, cvssConfidentialityImpact) <> (CvssRating.tupled, CvssRating.unapply)
  def cweOptionMapped = cweOption <> ((_: Option[String]).map(CWE.forIdentifierWithDescription), (_: Option[CWE]).map(CWE.unapply))
  def base = (cve, description, cweOptionMapped, cvssRating) <> (Vulnerability.tupled, Vulnerability.unapply)

  def * = (id, base)
}