package models.odc

import models.odc.profile.api._
import models.odc.profile.jdbcTypeFor
import slick.ast.TypedType
import models.odc.profile.MappedJdbcType
import slick.jdbc.JdbcType

import scala.reflect.ClassTag

// TODO: consider renaming to CpeEntryVulnerability or something like that
final case class SoftwareVulnerability (vulnerabilityId: Int, cpeEntryId: Int, includesAllPreviousVersionsRaw: Option[String]){
  def includesAllPreviousVersions: Boolean = includesAllPreviousVersionsRaw match {
    case Some("1") => true
    case None => false
    case _ => sys.error("Unexpected value from ODC database")
  }
}

/*private class OdcBooleanType(implicit t: JdbcType[Option[String]]) extends MappedJdbcType[Boolean, Option[String]] {
  override def map(t: Boolean): Option[String] = t match {
    case true => Some("1")
    case false => None
  }

  override def comap(u: Option[String]): Boolean = u match {
    case Some("1") => true
    case None => false
  }

}*/

class SoftwareVulnerabilities(tag: Tag) extends Table[SoftwareVulnerability](tag, "software") {
  def vulnerabilityId = column[Int]("cveid")
  def cpeEntryId = column[Int]("cpeEntryId")
  //private val bt = new OdcBooleanType()(jdbcTypeFor(implicitly[BaseColumnType[String]].optionType).asInstanceOf[JdbcType[Option[String]]])
  //MappedJdbcType.base[Boolean, Option[String]](???, ???)(implicitly[ClassTag[Boolean]], )
  def includesAllPreviousVersionsRaw = column[String]("previousVersion").?
  def * = (vulnerabilityId, cpeEntryId, includesAllPreviousVersionsRaw) <> (SoftwareVulnerability.tupled, SoftwareVulnerability.unapply)
}