/*
 *  Wave, containers provisioning service
 *  Copyright (c) 2023-2024, Seqera Labs
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package io.seqera.wave.service.scan


import groovy.transform.Canonical
import groovy.transform.CompileStatic
import groovy.transform.ToString
import groovy.util.logging.Slf4j
import io.seqera.wave.util.StringUtils
import org.jetbrains.annotations.NotNull
/**
 * Model for Scan Vulnerability
 *
 * @author Munish Chouhan <munish.chouhan@seqera.io>
 * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
 */
@Slf4j
@ToString(includeNames = true, includePackage = false)
@Canonical
@CompileStatic
class ScanVulnerability implements Comparable<ScanVulnerability> {

    static final Map<String,Integer> ORDER = Map.of(
            'LOW', 0,
            'MEDIUM', 1,
            'HIGH', 2,
            'CRITICAL', 3 )

    String id
    String severity
    String title
    String pkgName
    String installedVersion
    String fixedVersion
    String primaryUrl

    ScanVulnerability(){}

    ScanVulnerability(String id, String severity, String title, String pkgName, String installedVersion, String fixedVersion, String primaryUrl) {
        this.id = StringUtils.surrealId(id)
        this.severity = severity
        this.title = title
        this.pkgName = pkgName
        this.installedVersion = installedVersion
        this.fixedVersion = fixedVersion
        this.primaryUrl = primaryUrl
    }

    void setId(String id) {
        this.id = StringUtils.surrealId(id)
    }

    @Override
    int compareTo(@NotNull ScanVulnerability that) {
        if( that.severity==null ) return 1
        return ORDER[this.severity] <=> ORDER[that.severity]
    }

}
