# frozen_string_literal: true

class CisaKnownExploit < ActiveRecord::Base
  def self.find_by_id(id)
    find_by(cve_id: id)
  end

  #TODO: i think i can just use postgrs sql jsonb ->> queries to better pull this data
  def self.cve_id(cve_id)
    last.vulnerabilities.select { |vuln| vuln if vuln.dig('cve_id') == cve_id }
  end

  def self.cves_from_year(year)
    last.vulnerabilities.where("cve_id = 'CVE-#{year}-%'")
    #last.vulnerabilities.select { |vuln| vuln if vuln.dig('cve_id') =~ /CVE-#{year}-\d{4,7}/ }
  end

  def self.by_product(product_name)
    last.vulnerabilities.select {|vuln| vuln if vuln.dig('product') =~ /#{product_name}/ }
  end

  def self.by_due_date(due_date)
    last.vulnerabilities.select {|vuln| vuln if vuln.dig('due_date') == due_date }
  end
end